Skip to content

pandas 0.23 broke unary negative expression on Decimal data type #21380

Closed
@rbu

Description

@rbu

Code Sample, a copy-pastable example if possible

import pandas as pd
from decimal import Decimal as D
series = pd.Series([D(1)])
print(series)
print(-(series))

Problem description

I'm dealing with decimal data where exact representation is required, thus I use Python's Decimal type with pandas. With the update from 0.22 to 0.23, the unary negative expression broke.

Expected Output (from 0.22)

>>> import pandas as pd
>>> from decimal import Decimal as D
>>> series = pd.Series([D(1)])
>>> print(series)
0    1
dtype: object
>>> print(-(series))
0    -1
dtype: object

Actual Output (from 0.23)

>>> import pandas as pd
>>> from decimal import Decimal as D
>>> series = pd.Series([D(1)])
>>> print(series)
0    1
dtype: object
>>> print(-(series))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "python3.6/site-packages/pandas/core/generic.py", line 1124, in __neg__
    .format(values.dtype))
TypeError: Unary negative expects numeric dtype, not object

Workaround (in 0.23)

Broadcasting against 0 has the expected effect:

>>> 0-series
0    -1
dtype: object
>>> (0-series).iloc[0]
Decimal('-1')

Output of pd.show_versions()

>>> pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.16.13-300.fc28.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.0
pytest: None
pip: 9.0.3
setuptools: 38.5.1
Cython: None
numpy: 1.14.4
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: 0.4.1
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: 1.1.15
pymysql: None
psycopg2: 2.7.3.2 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Dtype ConversionsUnexpected or buggy dtype conversionsRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions