Skip to content

Empty dataset: typerror ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('O') #13844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
VelizarVESSELINOV opened this issue Jul 29, 2016 · 4 comments
Labels
Bug Datetime Datetime data dtype Timedelta Timedelta data type
Milestone

Comments

@VelizarVESSELINOV
Copy link

Code Sample, a copy-pastable example if possible

from pandas import DataFrame, to_datetime, to_timedelta


data = {'Item': [1], 'Date_Time': ['2/8/2015  6:00:30'], 'Relative_Time': 20}
dtf = DataFrame(data=data)

print(dtf)
dtf = dtf[dtf.Item == 2]  # if the filter returns empty dataset expected results will be that the same

dtf.Date_Time = to_datetime(dtf.Date_Time, infer_datetime_format=True)
dtf.Relative_Time = to_timedelta(dtf.Relative_Time, unit='s')
dtf.Date_Time = dtf.Date_Time + dtf.Relative_Time
print(dtf)

Current Output

          Date_Time  Item  Relative_Time
0  2/8/2015  6:00:30     1             20
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/ops.py:477: PerformanceWarning: Adding/subtracting array of DateOffsets to Series not vectorized
  "Series not vectorized", PerformanceWarning)
Traceback (most recent call last):
  File "bug.py", line 12, in <module>
    dtf.Date_Time = dtf.Date_Time + dtf.Relative_Time
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/ops.py", line 641, in wrapper
    arr = na_op(lvalues, rvalues)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/ops.py", line 481, in <lambda>
    self.na_op = lambda x, y: getattr(x, self.name)(y)
TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('O')

Expected Output

          Date_Time  Item  Relative_Time
0  2/8/2015  6:00:30     1             20
Empty DataFrame
Columns: [Date_Time, Item, Relative_Time]
Index: []

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Darwin
OS-release: 15.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.18.1
nose: None
pip: 8.1.2
setuptools: 24.0.3
Cython: None
numpy: 1.11.1
scipy: 0.16.1
statsmodels: None
xarray: None
IPython: 4.0.1
sphinx: None
patsy: None
dateutil: 2.5.3
pytz: 2016.6
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.0
openpyxl: 2.3.5
xlrd: 1.0.0
xlwt: 1.0.0
xlsxwriter: None
lxml: None
bs4: 4.4.1
html5lib: None
httplib2: 0.9.2
apiclient: 1.5.0
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None
None
statsmodels: None
xarray: None
IPython: 4.0.1
sphinx: None
patsy: None
dateutil: 2.5.3
pytz: 2016.6
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.0
openpyxl: 2.3.5
xlrd: 1.0.0
xlwt: 1.0.0
xlsxwriter: None
lxml: None
bs4: 4.4.1
html5lib: None
httplib2: 0.9.2
apiclient: 1.5.0
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None
None
@jreback
Copy link
Contributor

jreback commented Jul 29, 2016

Here is a simpler replication.

In [63]: df = pd.DataFrame({'A' : Series(dtype='M8[ns]'), 'B' : Series(dtype='m8[ns]')})

In [64]: df
Out[64]: 
Empty DataFrame
Columns: [A, B]
Index: []

In [65]: df.dtypes
Out[65]: 
A     datetime64[ns]
B    timedelta64[ns]
dtype: object

In [66]: df.A+df.B
TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('O')

Yeah this empty case is prob address by coercion, maybe @sinhrks has a better idea.

@jreback jreback added Bug Datetime Datetime data dtype Timedelta Timedelta data type Difficulty Intermediate labels Jul 29, 2016
@jreback jreback added this to the Next Major Release milestone Jul 29, 2016
@sinhrks
Copy link
Member

sinhrks commented Jul 30, 2016

The op is performed in _TimeOp class, and it regards empty input as offset even if it has a dtype.

Then, offset is coerced to object here.

Fixing _TimeOp._is_offset should solve the problem. PR is appreciated!

@tom-bird
Copy link
Contributor

tom-bird commented Aug 2, 2016

Happy to take a look at this and submit a PR

@tom-bird
Copy link
Contributor

tom-bird commented Aug 3, 2016

So, we shouldn't regard something as offset if its empty? This is a three line fix, and certainly resolves the error in the above two examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants