-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: pd.Timedelta does not accept np.int32, etc. #8757
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
Comments
hmm be a little too strict in this but sure easy enough to add interested in doing a pull-request? |
out of curiosity did you manually download? this should not be visible yet in pypi |
pandas 0.15.1? I just did a I'll take a shot at the pull request. |
Further documentation... >>> import pandas as pd
>>> import numpy as np
>>> pd.Timedelta(days=np.int64(1))
Traceback (most recent call last):
File "pandas/tslib.pyx", line 1636, in pandas.tslib.Timedelta.__new__ (pandas/tslib.c:27653)
TypeError: unsupported type for timedelta days component: numpy.int64
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pandas/tslib.pyx", line 1638, in pandas.tslib.Timedelta.__new__ (pandas/tslib.c:27693)
ValueError: cannot construct a TimeDelta from the passed arguments, allowed keywords are [days, seconds, microseconds, milliseconds, minutes, hours, weeks]
>>> pd.Timedelta(days=1)
Timedelta('1 days 00:00:00')
>>> pd.Timedelta(days=np.float64(1))
Timedelta('1 days 00:00:00')
>>> pd.Timedelta(days=np.float32(1))
Traceback (most recent call last):
File "pandas/tslib.pyx", line 1636, in pandas.tslib.Timedelta.__new__ (pandas/tslib.c:27653)
TypeError: unsupported type for timedelta days component: numpy.float32
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pandas/tslib.pyx", line 1638, in pandas.tslib.Timedelta.__new__ (pandas/tslib.c:27693)
ValueError: cannot construct a TimeDelta from the passed arguments, allowed keywords are [days, seconds, microseconds, milliseconds, minutes, hours, weeks]
>>> pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.4.2.final.0
python-bits: 64
OS: Linux
OS-release: 2.6.32-431.17.1.el6.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
pandas: 0.15.1
nose: 1.3.4
Cython: None
numpy: 1.9.1
scipy: 0.14.0
statsmodels: None
IPython: 2.3.0
sphinx: None
patsy: None
dateutil: 2.2
pytz: 2014.9
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.4.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
rpy2: None
sqlalchemy: None
pymysql: None
psycopg2: None |
ack. sorry bout the close. |
floats are legit, mostly, eg.
not 100% sure about accepting this though:
though |
Now that I've looked at the source code, I see that it's failing when trying to construct a python In [13]: datetime.timedelta(days=1)
Out[13]: datetime.timedelta(1)
In [14]: datetime.timedelta(days=1.0)
Out[14]: datetime.timedelta(1)
In [15]: datetime.timedelta(days=np.int64(1.0))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-194c3cc228db> in <module>()
----> 1 datetime.timedelta(days=np.int64(1.0))
TypeError: unsupported type for timedelta days component: numpy.int64
In [16]: datetime.timedelta(days=np.float64(1.0))
Out[16]: datetime.timedelta(1)
In [17]: datetime.timedelta(days=np.float32(1.0))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-2055ee731de0> in <module>()
----> 1 datetime.timedelta(days=np.float32(1.0))
TypeError: unsupported type for timedelta days component: numpy.float32
In [19]: datetime.timedelta(dayss=1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-19-699cc4692f32> in <module>()
----> 1 datetime.timedelta(dayss=1)
TypeError: 'dayss' is an invalid keyword argument for this function I'm not sure if it's appropriate to provide a work around here or not. At the very least, I think that the exception text should be made more accurate. It's a little tricky because both exceptions are classified as TypeErrors. One possibility would be to simply remove the try/except on line 1635 and let the python message through. Another possibility would be to check the python exception text for 'is an invalid keyword' or similar, and if true, raise the current pandas error, and if false, raise the error verbatim. Thoughts? |
see here: https://github.com/pydata/pandas/blob/master/pandas/tslib.pyx i would just iterate on the kwargs and coerce the not-None values to ints here (side issue is what to do with a float that is not == to its int value). |
BUG: pd.Timedelta np.int, np.float. fixes #8757
I'll open a PR, similar to #8787 when I can find the time. |
pd.Timedelta is a great addition, thanks!
Is it the intended behavior of pd.Timedelta to only accept np.int64, python ints, and python floats? I ran into this when I tried to construct a list of pd.Timedeltas from the np.int32 array returned by pd.DatetimeIndex.dayofyear.
Here's my version info:
The text was updated successfully, but these errors were encountered: