Skip to content

CLN: PeriodIndex to use nan related cache #13975

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
_ensure_object)

from pandas.types.generic import ABCSeries
from pandas.types.missing import isnull


import pandas.tseries.frequencies as frequencies
from pandas.tseries.frequencies import get_freq_code as _gfc
Expand All @@ -40,7 +38,6 @@
from pandas.util.decorators import Appender, cache_readonly, Substitution
from pandas.lib import Timedelta
import pandas.tslib as tslib
import pandas.core.missing as missing
from pandas.compat import zip, u


Expand Down Expand Up @@ -87,8 +84,7 @@ def wrapper(self, other):

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

mask = (missing.mask_missing(self.values, tslib.iNaT) |
missing.mask_missing(other.values, tslib.iNaT))
mask = self._isnan | other._isnan
if mask.any():
result[mask] = nat_result

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

mask = self.values == tslib.iNaT
if mask.any():
result[mask] = nat_result
if self.hasnans:
result[self._isnan] = nat_result

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

if self.hasnans:
mask = asi8 == tslib.iNaT
new_data[mask] = tslib.iNaT
new_data[self._isnan] = tslib.iNaT

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

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

if self.hasnans:
mask = asi8 == tslib.iNaT
new_data = new_data.astype(np.float64)
new_data[mask] = np.nan
new_data[self._isnan] = np.nan
# result must be Int64Index or Float64Index
return Index(new_data, name=self.name)

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

values = np.array(list(self), dtype=object)
mask = isnull(self.values)
values[mask] = na_rep
imask = ~mask
values = self.asobject.values

if date_format:
formatter = lambda dt: dt.strftime(date_format)
else:
formatter = lambda dt: u('%s') % dt
values[imask] = np.array([formatter(dt) for dt in values[imask]])

if self.hasnans:
mask = self._isnan
values[mask] = na_rep
imask = ~mask
values[imask] = np.array([formatter(dt) for dt
in values[imask]])
else:
values = np.array([formatter(dt) for dt in values])
return values

def append(self, other):
Expand Down