Skip to content

Commit 2b3041e

Browse files
committed
Don't use column_names internally
- Use columns instead
1 parent ee3ac7b commit 2b3041e

9 files changed

Lines changed: 31 additions & 27 deletions

File tree

datamatrix/_datamatrix/_datamatrix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ def __setstate__(self, state):
745745

746746
def __dir__(self):
747747

748-
return self.column_names + object.__dir__(self)
748+
return self.columns + object.__dir__(self)
749749

750750
def __contains__(self, item):
751751

datamatrix/_datamatrix/_row.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def as_slice(self):
4848
@property
4949
def column_names(self):
5050
return self._datamatrix.column_names
51+
52+
@property
53+
def columns(self):
54+
return self._datamatrix.columns
5155

5256
def equals(self, other):
5357

@@ -67,7 +71,7 @@ def equals(self, other):
6771

6872
def __dir__(self):
6973

70-
return self.column_names + object.__dir__(self)
74+
return self.columns + object.__dir__(self)
7175

7276
def __len__(self):
7377

@@ -80,7 +84,7 @@ def __getattr__(self, key):
8084
def __getitem__(self, key):
8185

8286
if isinstance(key, int):
83-
key = self._datamatrix.column_names[key]
87+
key = self._datamatrix.columns[key]
8488
return self._datamatrix[key][self._index]
8589

8690
def __setattr__(self, key, value):
@@ -90,11 +94,11 @@ def __setattr__(self, key, value):
9094
def __setitem__(self, key, value):
9195

9296
if isinstance(key, int):
93-
key = self._datamatrix.column_names[key]
97+
key = self._datamatrix.columns[key]
9498
elif isinstance(key, str):
9599
# Create a new column with default values if the column does not
96100
# exist yet
97-
if key not in self._datamatrix.column_names:
101+
if key not in self._datamatrix.columns:
98102
self._datamatrix[key] = \
99103
self._datamatrix._default_col_type.default_value
100104
self._datamatrix[key][self._index] = value
@@ -109,5 +113,5 @@ def __str__(self):
109113

110114
def __iter__(self):
111115

112-
for col in self._datamatrix.column_names:
116+
for col in self._datamatrix.columns:
113117
yield col, self[col]

datamatrix/io/_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,6 @@ def writetxt(dm, path, delimiter=',', quotechar='"'):
114114
quotechar=quotechar,
115115
lineterminator='\n'
116116
)
117-
writer.writerow([utils.safe_decode(colname) for colname in dm.column_names])
117+
writer.writerow([utils.safe_decode(colname) for colname in dm.columns])
118118
for row in dm:
119119
writer.writerow([utils.safe_decode(value) for colname, value in row])

datamatrix/operations.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def weight(col):
309309
% (weight, type(weight))
310310
)
311311
for c in range(weight):
312-
for colname in dm1.column_names:
312+
for colname in dm1.columns:
313313
dm2[colname][i2] = dm1[colname][i1]
314314
i2 += 1
315315
return dm2
@@ -555,11 +555,11 @@ def fullfactorial(dm, ignore=u''):
555555
a = _fullfact(design)
556556
# Create an DataMatrix with empty columns
557557
fdm = DataMatrix(a.shape[0])
558-
for name in dm.column_names:
558+
for name in dm.columns:
559559
fdm[name] = u''
560560
for i in range(a.shape[0]):
561561
row = a[i]
562-
for rownr, name in enumerate(dm.column_names):
562+
for rownr, name in enumerate(dm.columns):
563563
fdm[name][i] = dm[name][int(row[rownr])]
564564
return fdm
565565

@@ -888,9 +888,9 @@ def keep_only(dm, *cols):
888888
) % colname
889889
)
890890
for colname in colnames:
891-
if colname not in dm.column_names:
891+
if colname not in dm.columns:
892892
utils.logger.warning('no column named {}'.format(colname))
893-
for colname in dm.column_names:
893+
for colname in dm.columns:
894894
if colname not in colnames:
895895
del dm[colname]
896896
return dm

doc-pelican/content/pages/basic.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ for colname in dm.columns:
4242
for cell in dm[colname]: # Loop through all cells in the column
4343
print(cell) # do something with the cell
4444
# Or just see which columns exist
45-
print(dm.column_names)
45+
print(dm.columns)
4646
~~~
4747

4848
__Important note:__ Because of a limitation (or feature, if you will) of the Python language, the behavior of `and`, `or`, and chained (`x < y < z`) comparisons cannot be modified. These therefore do not work with `DataMatrix` objects as you would expect them to:
@@ -548,7 +548,7 @@ The `column_names` property gives a sorted list of all column names (without the
548548

549549

550550
```python
551-
print(dm.column_names)
551+
print(dm.columns)
552552
```
553553

554554

doc-pelican/content/pages/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ for colname in dm.columns:
7070
for cell in dm[colname]: # Loop through all cells in the column
7171
print(cell) # do something with the cell
7272
# Or just see which columns exist
73-
print(dm.column_names)
73+
print(dm.columns)
7474
~~~

testcases/test_dataframe_compatibility.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_dict_initialization():
2626
"""Data can be passed as a dict at construction time."""
2727
for cls in TEST_CLASSES:
2828
obj = _make_sample(cls)
29-
assert set(obj.columns if cls is pd.DataFrame else obj.column_names) == {"A", "B", "C"}
29+
assert set(obj.columns if cls is pd.DataFrame else obj.columns) == {"A", "B", "C"}
3030

3131

3232
def test_list_of_dicts_initialization():
@@ -41,7 +41,7 @@ def test_list_of_dicts_initialization():
4141
for cls in TEST_CLASSES:
4242
obj = cls(data)
4343
# Check column names
44-
assert set(obj.columns if cls is pd.DataFrame else obj.column_names) == {"A", "B", "C"}
44+
assert set(obj.columns if cls is pd.DataFrame else obj.columns) == {"A", "B", "C"}
4545
# Check data integrity
4646
assert list(obj["A"]) == [1, 2, 3, 4]
4747
assert obj['A'].dtype == int
@@ -63,7 +63,7 @@ def test_list_of_dicts_with_missing_keys():
6363
for cls in TEST_CLASSES:
6464
obj = cls(data)
6565
# Check column names
66-
assert set(obj.columns if cls is pd.DataFrame else obj.column_names) == {"A", "B", "C"}
66+
assert set(obj.columns if cls is pd.DataFrame else obj.columns) == {"A", "B", "C"}
6767
# Check that missing values are handled (None or NaN)
6868
assert len(obj) == 4
6969
# Note: DataFrame uses NaN for missing numeric values and None for objects
@@ -75,7 +75,7 @@ def test_empty_list_initialization():
7575
for cls in TEST_CLASSES:
7676
obj = cls([])
7777
assert len(obj) == 0
78-
assert len(obj.columns if cls is pd.DataFrame else obj.column_names) == 0
78+
assert len(obj.columns if cls is pd.DataFrame else obj.columns) == 0
7979

8080

8181
def test_list_of_empty_dicts():
@@ -84,7 +84,7 @@ def test_list_of_empty_dicts():
8484
for cls in TEST_CLASSES:
8585
obj = cls(data)
8686
assert len(obj) == 3
87-
assert len(obj.columns if cls is pd.DataFrame else obj.column_names) == 0
87+
assert len(obj.columns if cls is pd.DataFrame else obj.columns) == 0
8888

8989
def test_multi_column_selection():
9090
"""Selecting a list of columns returns the expected subset."""

testcases/test_extra_operations.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,13 @@ def test_keep_only():
368368
dm.c = 'y', 'z'
369369
for cols in (['b', 'c'], [dm.b, dm.c]):
370370
dm = ops.keep_only(dm, *cols)
371-
assert 'a' not in dm.column_names
372-
assert 'b' in dm.column_names
373-
assert 'c' in dm.column_names
371+
assert 'a' not in dm.columns
372+
assert 'b' in dm.columns
373+
assert 'c' in dm.columns
374374
dm = dm[cols]
375-
assert 'a' not in dm.column_names
376-
assert 'b' in dm.column_names
377-
assert 'c' in dm.column_names
375+
assert 'a' not in dm.columns
376+
assert 'b' in dm.columns
377+
assert 'c' in dm.columns
378378

379379

380380
def test_auto_type():

testcases/test_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def all_nan(*l):
3434

3535
def check_dm(dm, ref):
3636

37-
assert dm.column_names == ref.column_names
37+
assert dm.columns == ref.columns
3838
for column_name in dm.columns:
3939
assert not isinstance(column_name, bytes)
4040
for colname in dm.columns:

0 commit comments

Comments
 (0)