Skip to content

Commit 70f361b

Browse files
sinhrksjreback
authored andcommitted
CLN: PeriodIndex to use nan related cache
Author: sinhrks <[email protected]> Closes #13975 from sinhrks/period_mask and squashes the following commits: 60b3351 [sinhrks] CLN: PeriodIndex to use nan related cache
1 parent 4a80521 commit 70f361b

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

pandas/tseries/period.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
_ensure_object)
1717

1818
from pandas.types.generic import ABCSeries
19-
from pandas.types.missing import isnull
20-
2119

2220
import pandas.tseries.frequencies as frequencies
2321
from pandas.tseries.frequencies import get_freq_code as _gfc
@@ -40,7 +38,6 @@
4038
from pandas.util.decorators import Appender, cache_readonly, Substitution
4139
from pandas.lib import Timedelta
4240
import pandas.tslib as tslib
43-
import pandas.core.missing as missing
4441
from pandas.compat import zip, u
4542

4643

@@ -87,8 +84,7 @@ def wrapper(self, other):
8784

8885
result = getattr(self.values, opname)(other.values)
8986

90-
mask = (missing.mask_missing(self.values, tslib.iNaT) |
91-
missing.mask_missing(other.values, tslib.iNaT))
87+
mask = self._isnan | other._isnan
9288
if mask.any():
9389
result[mask] = nat_result
9490

@@ -101,9 +97,8 @@ def wrapper(self, other):
10197
func = getattr(self.values, opname)
10298
result = func(other.ordinal)
10399

104-
mask = self.values == tslib.iNaT
105-
if mask.any():
106-
result[mask] = nat_result
100+
if self.hasnans:
101+
result[self._isnan] = nat_result
107102

108103
return result
109104
return wrapper
@@ -498,8 +493,7 @@ def asfreq(self, freq=None, how='E'):
498493
new_data = period.period_asfreq_arr(ordinal, base1, base2, end)
499494

500495
if self.hasnans:
501-
mask = asi8 == tslib.iNaT
502-
new_data[mask] = tslib.iNaT
496+
new_data[self._isnan] = tslib.iNaT
503497

504498
return self._simple_new(new_data, self.name, freq=freq)
505499

@@ -637,9 +631,8 @@ def _sub_period(self, other):
637631
new_data = asi8 - other.ordinal
638632

639633
if self.hasnans:
640-
mask = asi8 == tslib.iNaT
641634
new_data = new_data.astype(np.float64)
642-
new_data[mask] = np.nan
635+
new_data[self._isnan] = np.nan
643636
# result must be Int64Index or Float64Index
644637
return Index(new_data, name=self.name)
645638

@@ -892,16 +885,21 @@ def __getitem__(self, key):
892885
def _format_native_types(self, na_rep=u('NaT'), date_format=None,
893886
**kwargs):
894887

895-
values = np.array(list(self), dtype=object)
896-
mask = isnull(self.values)
897-
values[mask] = na_rep
898-
imask = ~mask
888+
values = self.asobject.values
899889

900890
if date_format:
901891
formatter = lambda dt: dt.strftime(date_format)
902892
else:
903893
formatter = lambda dt: u('%s') % dt
904-
values[imask] = np.array([formatter(dt) for dt in values[imask]])
894+
895+
if self.hasnans:
896+
mask = self._isnan
897+
values[mask] = na_rep
898+
imask = ~mask
899+
values[imask] = np.array([formatter(dt) for dt
900+
in values[imask]])
901+
else:
902+
values = np.array([formatter(dt) for dt in values])
905903
return values
906904

907905
def append(self, other):

0 commit comments

Comments
 (0)