Skip to content

Commit a344b97

Browse files
committed
Fix decoding of column names during assignment
1 parent 3ed12ee commit a344b97

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

datamatrix/_datamatrix/_datamatrix.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
along with datamatrix. If not, see <http://www.gnu.org/licenses/>.
1818
"""
1919

20-
import warnings
21-
from datamatrix import Row
20+
from datamatrix import Row, utils
2221
from datamatrix._datamatrix._basecolumn import BaseColumn
2322
from datamatrix._datamatrix._mixedcolumn import MixedColumn
2423
from datamatrix._datamatrix._index import Index
@@ -166,9 +165,9 @@ def column_names(self):
166165
167166
Use DataMatrix.columns instead.
168167
"""
169-
warnings.warn(
168+
utils.logger.warning(
170169
'DataMatrix.column_names is deprecated, use DataMatrix.columns '
171-
'instead', DeprecationWarning)
170+
'instead')
172171
return self._to_list(self._cols.keys())
173172

174173
@property
@@ -529,7 +528,7 @@ def _getcolbyname(self, key):
529528
"""
530529

531530
if isinstance(key, bytes):
532-
key = safe_decode(key)
531+
key = utils.safe_decode(key)
533532
col = self._cols.get(key, None)
534533
if col is None:
535534
raise AttributeError(u'No column named "%s"' % key)
@@ -587,7 +586,7 @@ def _set_col(self, name, value):
587586

588587
# Check if this is a valid column name
589588
if isinstance(name, bytes):
590-
name = safe_decode(name)
589+
name = utils.safe_decode(name)
591590
if not isinstance(name, str):
592591
raise TypeError(u'Column names should be str, not %s' % type(name))
593592
# Create a new column by column type:
@@ -753,7 +752,7 @@ def __delattr__(self, name):
753752
def __setattr__(self, name, value):
754753

755754
if isinstance(name, bytes):
756-
name = safe_decode(name)
755+
name = utils.safe_decode(name)
757756
if name == u'length':
758757
self._setlength(value)
759758
return
@@ -900,12 +899,6 @@ def __array__(self, *args):
900899
pass
901900
return a
902901

903-
def __dataframe__(self):
904-
905-
from datamatrix import convert as cnv
906-
907-
return cnv.to_pandas(self)
908-
909902
def _instantiate(self):
910903
"""Instantiates all uninstantiated columns"""
911904
for name, col in self._cols.items():
@@ -924,3 +917,11 @@ def _instantiate_column(self, key, col=None):
924917
col = col.instantiate()
925918
self._cols[key] = col
926919
return col
920+
921+
# The functions below mimic the pandas.DataFrame API
922+
923+
def __dataframe__(self):
924+
925+
from datamatrix import convert as cnv
926+
927+
return cnv.to_pandas(self)

testcases/test_basics.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,14 @@ def test_resize():
639639
for x, y in zip(dm._rowid, range(l)):
640640
print(x, y)
641641
assert x == y
642+
643+
644+
def test_assignment():
645+
dm = DataMatrix(length=1)
646+
dm.a = 1
647+
dm['a'] = dm.a + 1
648+
dm[b'a'] = dm.a + 1
649+
assert dm.a[0] == 3
642650

643651

644652
def test_properties():

0 commit comments

Comments
 (0)