per = pd.Period('2016Q1')
ser = pd.Series([per])
>>> ser - pd.NaT
0 NaT
dtype: datetime64[ns]
>>> ser + pd.NaT
0 NaT
dtype: datetime64[ns]
Comment From: jreback
well , the + is right.
Comment From: makbigc
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
)?
Comment From: jreback
this should be similar to how we treat a -/+ with a Timestamp (i think we raise)?
Comment From: jbrockmendel
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)
Comment From: jbrockmendel
This is not correct and is tested by test_pi_sub_pdnat. Closing.