Skip to content

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

Merged
merged 1 commit into from
Nov 11, 2014

Conversation

wholmgren
Copy link
Contributor

closes #8757

@jreback jreback added Bug Dtype Conversions Unexpected or buggy dtype conversions Timedelta Timedelta data type labels Nov 11, 2014
@jreback jreback added this to the 0.15.2 milestone Nov 11, 2014
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)
Copy link
Contributor

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)

Copy link
Contributor Author

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]")
Copy link
Contributor

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'))

Copy link
Contributor Author

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:

  1. Remove my changes to the error reporting and let the numpy error reach the user.
  2. Move the kwargs = dict() line inside the try block and test for numpy's TypeError: data type "foo" not understood in the except block.

I'd go for the first option.

Copy link
Contributor

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)))

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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 :)

@wholmgren
Copy link
Contributor Author

Thanks for the pointers. Let me know if you want any other changes.

jreback added a commit that referenced this pull request Nov 11, 2014
BUG: pd.Timedelta np.int, np.float. fixes #8757
@jreback jreback merged commit 54e237b into pandas-dev:master Nov 11, 2014
@jreback
Copy link
Contributor

jreback commented Nov 11, 2014

perfect thanks!

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

Successfully merging this pull request may close these issues.

BUG: pd.Timedelta does not accept np.int32, etc.
2 participants