-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: pd.Timedelta np.int, np.float. fixes #8757 #8787
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
Conversation
self.assertEqual(Timedelta(days=10.0).value, expected) | ||
|
||
for npdtype in [np.int64, np.int32, np.int16, np.float64, np.float32, np.float16]: | ||
self.assertEqual(Timedelta(days=npdtype(10)).value, expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add another loop which tests each of the kwargs (e.g. days,minutes,seconds,milliseconds,microseconds,nanoseconds
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
"[days, seconds, microseconds, milliseconds, minutes, hours, weeks]") | ||
except TypeError as e: | ||
errormsg = ("cannot construct a TimeDelta from the passed arguments, allowed keywords are " | ||
"[days, seconds, microseconds, milliseconds, minutes, hours, weeks]") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfect. Can you put an example which triggers this last (e.g. Timedelta(days='foo')
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, actually, no. np.issubdtype
chokes on anything that's not a dtype, so I don't think that the additional error text is reachable. Two potential options:
- Remove my changes to the error reporting and let the numpy error reach the user.
- Move the
kwargs = dict()
line inside the try block and test for numpy'sTypeError: data type "foo" not understood
in the except block.
I'd go for the first option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better then to use com.is_integer/com.is_float
(where you have np.issubdtype
now, as these will always return properly) (so maybe just simpler to coerce directly)
e.g.
def convert(v):
if com.is_integer(v):
return int(v)
elif com.is_float(v):
return float(v)
raise ValueError("invalid type {0}".format(type(v)))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't know about these functions. It seems that I've run into a circular import problem, though. I added import pandas.core.common as com
to the top of tslib
and I get this when importing pandas:
File "/home/will/git_repos/pandas/pandas/__init__.py", line 7, in <module>
from . import hashtable, tslib, lib
File "pandas/tslib.pyx", line 41, in init pandas.tslib (pandas/tslib.c:77611)
import pandas.core.common as com
File "/home/will/git_repos/pandas/pandas/core/common.py", line 18, in <module>
import pandas.algos as algos
File "pandas/algos.pyx", line 64, in init pandas.algos (pandas/algos.c:181716)
from pandas import lib
File "pandas/lib.pyx", line 50, in init pandas.lib (pandas/lib.c:78075)
from tslib import NaT, Timestamp, Timedelta
ImportError: cannot import name NaT
Could we import is_float
, is_int
in core.api
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, forgot, these you are in cython already, use use is_intege_object/is_float_object
they are already defined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. One more question: Did you really want a ValueError for the convert function? I thought that TypeError would be more appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or sorry, TypeError
is right. Was a typo :)
Thanks for the pointers. Let me know if you want any other changes. |
BUG: pd.Timedelta np.int, np.float. fixes #8757
perfect thanks! |
closes #8757