Skip to content
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

Series[Period] +/- NaT returns Series[datetime64] #19389

Closed
jbrockmendel opened this issue Jan 25, 2018 · 5 comments
Closed

Series[Period] +/- NaT returns Series[datetime64] #19389

jbrockmendel opened this issue Jan 25, 2018 · 5 comments
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Numeric Operations Arithmetic, Comparison, and Logical operations Period Period data type Timedelta Timedelta data type

Comments

@jbrockmendel
Copy link
Member

per = pd.Period('2016Q1')
ser = pd.Series([per])

>>> ser - pd.NaT
0   NaT
dtype: datetime64[ns]

>>> ser + pd.NaT
0   NaT
dtype: datetime64[ns]
@jreback
Copy link
Contributor

jreback commented Jan 25, 2018

well , the + is right.

@jreback jreback added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Timedelta Timedelta data type Period Period data type Difficulty Intermediate labels Jan 25, 2018
@jreback jreback added this to the Next Major Release milestone Jan 25, 2018
@makbigc
Copy link
Contributor

makbigc commented Feb 7, 2019

Now, the behaviour is:

In [2]: per = pd.Period('2016Q1')

In [3]: ser = pd.Series([per])

In [4]: ser - pd.NaT
Out[4]: 
0   NaT
dtype: timedelta64[ns]

In [5]: ser + pd.NaT
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-b4d602b00491> in <module>()
----> 1 ser + pd.NaT

~/Code/pandas/pandas/core/ops.py in wrapper(left, right)
   1580                 (is_extension_array_dtype(right) and not is_scalar(right))):
   1581             # GH#22378 disallow scalar to exclude e.g. "category", "Int64"
-> 1582             return dispatch_to_extension_op(op, left, right)
   1583 
   1584         elif is_timedelta64_dtype(left):

~/Code/pandas/pandas/core/ops.py in dispatch_to_extension_op(op, left, right)
   1240         new_right = right
   1241 
-> 1242     res_values = op(new_left, new_right)
   1243     res_name = get_op_result_name(left, right)
   1244 

~/Code/pandas/pandas/core/arrays/datetimelike.py in __add__(self, other)
   1183         # scalar others
   1184         elif other is NaT:
-> 1185             result = self._add_nat()
   1186         elif isinstance(other, (Tick, timedelta, np.timedelta64)):
   1187             result = self._add_delta(other)

~/Code/pandas/pandas/core/arrays/datetimelike.py in _add_nat(self)
   1013             raise TypeError('Cannot add {cls} and {typ}'
   1014                             .format(cls=type(self).__name__,
-> 1015                                     typ=type(NaT).__name__))
   1016 
   1017         # GH#19124 pd.NaT is treated like a timedelta for both timedelta

TypeError: Cannot add PeriodArray and NaTType

Should the TypeError be thrown in the case of subtraction (func _sub_nat)?

@jreback
Copy link
Contributor

jreback commented Feb 7, 2019

this should be similar to how we treat a -/+ with a Timestamp (i think we raise)?

@jbrockmendel
Copy link
Member Author

The "correct" way to solve this would be to implement #24983.

The issue is in how we treat pd.NaT. In most cases, we treat it is a datetime. In a few cases we treat it like a timedelta. The rule seems to be "1) if it can be treated as a datetime without raising, do that. 2) otherwise treat it as a timedelta" but I don't think we're consistent about part 2 there (and am not sure we should be)

@jbrockmendel jbrockmendel added Numeric Operations Arithmetic, Comparison, and Logical operations and removed Difficulty Intermediate labels Oct 16, 2019
@mroeschke mroeschke added the Bug label May 3, 2020
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@jbrockmendel
Copy link
Member Author

This is not correct and is tested by test_pi_sub_pdnat. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Numeric Operations Arithmetic, Comparison, and Logical operations Period Period data type Timedelta Timedelta data type
Projects
None yet
Development

No branches or pull requests

4 participants