Skip to content

BUG: TDA.__floordiv__ with NaT #35583

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 2 commits into from
Aug 6, 2020
Merged

Conversation

jbrockmendel
Copy link
Member

@simonjayhawkins simonjayhawkins added Bug Numeric Operations Arithmetic, Comparison, and Logical operations Timedelta Timedelta data type labels Aug 6, 2020
@simonjayhawkins simonjayhawkins added this to the 1.2 milestone Aug 6, 2020
@simonjayhawkins
Copy link
Member

The return type of a floordiv with a scalar should be an integer. the cast is unavoidable at the moment. Maybe a TODO to change to nullable integer in the future and raising a RuntimeWarning in the meantime for consistency with NumPy.

>>> arr = np.array([1, 2, 3])
>>> arr
array([1, 2, 3])
>>>
>>> arr // arr
array([1, 1, 1], dtype=int32)
>>>
>>> arr2 = np.array([1, 2, np.nan])
>>> arr2
array([ 1.,  2., nan])
>>>
>>> arr // arr2
<stdin>:1: RuntimeWarning: invalid value encountered in floor_divide
array([ 1.,  1., nan])
>>>

@simonjayhawkins
Copy link
Member

otherwise LGTM

@simonjayhawkins
Copy link
Member

simonjayhawkins commented Aug 6, 2020

hang on a minute. NumPy now supports floordiv on timedeltas but returns an integer array.

>>> np.__version__
'1.19.1'
>>>
>>> left = np.array([1000, 222330, 30], dtype="timedelta64[ns]")
>>> left
array([  1000, 222330,     30], dtype='timedelta64[ns]')
>>>
>>> right = np.array([1000, 222330, None], dtype="timedelta64[ns]")
>>> right
array([  1000, 222330,  'NaT'], dtype='timedelta64[ns]')
>>>
>>> left // right
<stdin>:1: RuntimeWarning: invalid value encountered in floor_divide
array([1, 1, 0], dtype=int64)
>>>

UPDATED: this also raises a RuntimeWarning in a clean session.

@jbrockmendel
Copy link
Member Author

The return type of a floordiv with a scalar should be an integer

>>> ser = pd.Series([-1, 0, 1])
>>> ser // np.nan
0   NaN
1   NaN
2   NaN
dtype: float64

We also don't follow numpy exactly on this (though usually the issue is division by zero, not division by nan).

@jreback
Copy link
Contributor

jreback commented Aug 6, 2020

this looks fine. I think we return float arrays now in other ops (where as numpy does not cast), so think this is ok.

@jreback jreback merged commit f0e3f0a into pandas-dev:master Aug 6, 2020
@jbrockmendel jbrockmendel deleted the bug-floordiv branch August 6, 2020 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Numeric Operations Arithmetic, Comparison, and Logical operations Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: ValueError during floordiv of a series with timedelta type
3 participants