8000 WIP: DatetimeArray+TimedeltaArray by jbrockmendel · Pull Request #23415 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

WIP: DatetimeArray+TimedeltaArray #23415

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 10 commits into from
Prev Previous commit
Next Next commit
implement from_sequence, from_factorized, values_for_factore, copy
  • Loading branch information
jbrockmendel committed Oct 31, 2018
commit e309ab94ef38c77136fef733613c600761899b99
56 changes: 56 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def asi8(self):
# ------------------------------------------------------------------
# Array-like Methods

@property
def nbytes(self):
return self.asi8.nbytes

@property
def shape(self):
return (len(self),)
Expand Down Expand Up @@ -211,6 +215,31 @@ def astype(self, dtype, copy=True):

# ------------------------------------------------------------------
# ExtensionArray Interface
# isna
# __getitem__
# __len__
# nbytes
# take
# _concat_same_type
# copy
# _from_factorized
# factorize / _values_for_factorize
# _from_sequence
# unique
#
# dtype
#
# dropna
#
#* _formatting_values
#* fillna
#* argsort / _values_for_argsort
#* _reduce

def unique(self):
from pandas.core.algorithms import unique1d
result = unique1d(self.asi8)
return self._shallow_copy(result)

def _validate_fill_value(self, fill_value):
"""
Expand Down Expand Up @@ -254,9 +283,36 @@ def _concat_same_type(cls, to_concat):
values = np.concatenate([x._data for x in to_concat])
return cls._simple_new(values, freq=freq)

def copy(self, deep=False):
# TODO: should `deep` determine whether we copy self.asi8?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes like

values = self.asi8
if deep:
    values = values.copy()
....

if is_datetime64tz_dtype(self):
return type(self)(self.asi8.copy(), tz=self.tz, freq=self.freq)
return type(self)(self.asi8.copy(), freq=self.freq)

def _values_for_factorize(self):
return self.asi8, iNaT

@classmethod
def _from_factorized(cls, values, original):
if is_datetime64tz_dtype(original):
return cls(values, tz=original.tz, freq=original.freq)
return cls(values, freq=original.freq)

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy=False):
arr = np.asarray(scalars, dtype=object)
if copy:
arr = arr.copy()

# If necessary this will infer tz from dtype
return cls(arr, dtype=dtype)

# ------------------------------------------------------------------
# Null Handling

def isna(self):
return self._isnan

@property # NB: override with cache_readonly in immutable subclasses
def _isnan(self):
""" return if each value is nan"""
Expand Down
22 changes: 0 additions & 22 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,6 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
ordinals = libperiod.extract_ordinals(periods, freq)
return cls(ordinals, freq=freq)

def _values_for_factorize(self):
return self.asi8, iNaT

@classmethod
def _from_factorized(cls, values, original):
# type: (Sequence[Optional[Period]], PeriodArray) -> PeriodArray
return cls(values, freq=original.freq)

@classmethod
def _from_datetime64(cls, data, freq, tz=None):
"""Construct a PeriodArray from a datetime64 array
Expand Down Expand Up @@ -257,14 +249,6 @@ def _generate_range(cls, start, end, periods, freq, fields):

return subarr, freq

@classmethod
def _concat_same_type(cls, to_concat):
freq = {x.freq for x in to_concat}
assert len(freq) == 1
freq = list(freq)[0]
values = np.concatenate([x._data for x in to_concat])
return cls(values, freq=freq)

# --------------------------------------------------------------------
# Data / Attributes
@property
Expand Down Expand Up @@ -400,9 +384,6 @@ def take(self, indices, allow_fill=False, fill_value=None):

return type(self)(new_values, self.freq)

def isna(self):
return self._data == iNaT

def fillna(self, value=None, method=None, limit=None):
# TODO(#20300)
# To avoid converting to object, we re-implement here with the changes
Expand Down Expand Up @@ -438,9 +419,6 @@ def fillna(self, value=None, method=None, limit=None):
new_values = self.copy()
return new_values

def copy(self, deep=False):
return type(self)(self._data.copy(), freq=self.freq)

def value_counts(self, dropna=False):
from pandas import Series, PeriodIndex

Expand Down
0