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

REF: Delegate more methods for DTI/TDI/PI #30607

Merged
merged 3 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def _formatter(self, boxed=False):

return _get_format_timedelta64(self, box=True)

def _format_native_types(self, na_rep="NaT", date_format=None):
def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

do we actually use these kwargs (i know they are on the method you are removing)?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, without this we get a test failure bc float_format is passed

Copy link
Contributor

Choose a reason for hiding this comment

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

ahh i c, as these are fairly generic, ok then.

from pandas.io.formats.format import _get_format_timedelta64

formatter = _get_format_timedelta64(self._data, na_rep)
Expand Down
50 changes: 12 additions & 38 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime, time, timedelta
from datetime import datetime, time, timedelta, tzinfo
import operator
from typing import Optional
import warnings

import numpy as np
Expand Down Expand Up @@ -66,8 +67,13 @@ class DatetimeDelegateMixin(DatetimelikeDelegateMixin):
# We also have a few "extra" attrs, which may or may not be raw,
# which we we dont' want to expose in the .dt accessor.
_extra_methods = ["to_period", "to_perioddelta", "to_julian_date", "strftime"]
_extra_raw_methods = ["to_pydatetime", "_local_timestamps", "_has_same_tz"]
_extra_raw_properties = ["_box_func", "tz", "tzinfo"]
_extra_raw_methods = [
jreback marked this conversation as resolved.
Show resolved Hide resolved
"to_pydatetime",
"_local_timestamps",
"_has_same_tz",
"_format_native_types",
]
_extra_raw_properties = ["_box_func", "tz", "tzinfo", "dtype"]
_delegated_properties = DatetimeArray._datetimelike_ops + _extra_raw_properties
_delegated_methods = (
DatetimeArray._datetimelike_methods + _extra_methods + _extra_raw_methods
Expand All @@ -88,7 +94,7 @@ class DatetimeDelegateMixin(DatetimelikeDelegateMixin):
DatetimeArray,
DatetimeDelegateMixin._delegated_methods,
typ="method",
overwrite=False,
overwrite=True,
)
class DatetimeIndex(DatetimeTimedeltaMixin, DatetimeDelegateMixin):
"""
Expand Down Expand Up @@ -197,8 +203,6 @@ class DatetimeIndex(DatetimeTimedeltaMixin, DatetimeDelegateMixin):
_engine_type = libindex.DatetimeEngine
_supports_partial_string_indexing = True

_tz = None
_freq = None
_comparables = ["name", "freqstr", "tz"]
_attributes = ["name", "tz", "freq"]

Expand All @@ -214,6 +218,8 @@ class DatetimeIndex(DatetimeTimedeltaMixin, DatetimeDelegateMixin):
_datetimelike_ops = DatetimeArray._datetimelike_ops
_datetimelike_methods = DatetimeArray._datetimelike_methods

tz: Optional[tzinfo]

# --------------------------------------------------------------------
# Constructors

Expand Down Expand Up @@ -310,25 +316,6 @@ def __array__(self, dtype=None):
dtype = "M8[ns]"
return np.asarray(self._data, dtype=dtype)

@property
def dtype(self):
return self._data.dtype

@property
def tz(self):
# GH 18595
return self._data.tz

@tz.setter
def tz(self, value):
# GH 3746: Prevent localizing or converting the index by setting tz
raise AttributeError(
"Cannot directly set timezone. Use tz_localize() "
"or tz_convert() as appropriate"
)

tzinfo = tz

@cache_readonly
def _is_dates_only(self) -> bool:
"""
Expand Down Expand Up @@ -401,15 +388,6 @@ def _mpl_repr(self):
# how to represent ourselves to matplotlib
return libts.ints_to_pydatetime(self.asi8, self.tz)

def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
from pandas.io.formats.format import _get_format_datetime64_from_values

fmt = _get_format_datetime64_from_values(self, date_format)

return libts.format_array_from_datetime(
self.asi8, tz=self.tz, format=fmt, na_rep=na_rep
)

@property
def _formatter_func(self):
from pandas.io.formats.format import _get_format_datetime64
Expand Down Expand Up @@ -999,10 +977,6 @@ def __getitem__(self, key):
return result
return type(self)(result, name=self.name)

@property
def _box_func(self):
return lambda x: Timestamp(x, tz=self.tz)

# --------------------------------------------------------------------

@Substitution(klass="DatetimeIndex")
Expand Down
19 changes: 5 additions & 14 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ class PeriodDelegateMixin(DatetimelikeDelegateMixin):
"""

_delegate_class = PeriodArray
_delegated_properties = PeriodArray._datetimelike_ops
_delegated_methods = set(PeriodArray._datetimelike_methods) | {
"_addsub_int_array",
"strftime",
}
_raw_properties = {"is_leap_year"}
_raw_methods = {"_format_native_types"}
_raw_properties = {"is_leap_year", "freq"}

_delegated_properties = PeriodArray._datetimelike_ops + list(_raw_properties)
_delegated_methods = set(PeriodArray._datetimelike_methods) | _raw_methods


@delegate_names(PeriodArray, PeriodDelegateMixin._delegated_properties, typ="property")
Expand Down Expand Up @@ -262,10 +261,6 @@ def _simple_new(cls, values, name=None, freq=None, **kwargs):
def values(self):
return np.asarray(self)

@property
def freq(self) -> DateOffset:
return self._data.freq

def _shallow_copy(self, values=None, **kwargs):
# TODO: simplify, figure out type of values
if values is None:
Expand Down Expand Up @@ -363,10 +358,6 @@ def _maybe_convert_timedelta(self, other):
# ------------------------------------------------------------------------
# Rendering Methods

def _format_native_types(self, na_rep="NaT", quoting=None, **kwargs):
# just dispatch, return ndarray
return self._data._format_native_types(na_rep=na_rep, quoting=quoting, **kwargs)

def _mpl_repr(self):
# how to represent ourselves to matplotlib
return self.astype(object).values
Expand Down
34 changes: 9 additions & 25 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,15 @@ class TimedeltaDelegateMixin(DatetimelikeDelegateMixin):
# We also have a few "extra" attrs, which may or may not be raw,
# which we don't want to expose in the .dt accessor.
_delegate_class = TimedeltaArray
_delegated_properties = TimedeltaArray._datetimelike_ops + ["components"]
_delegated_methods = TimedeltaArray._datetimelike_methods + [
"_box_values",
"__neg__",
"__pos__",
"__abs__",
"sum",
"std",
"median",
]
_raw_properties = {"components"}
_raw_methods = {"to_pytimedelta", "sum", "std", "median"}
_raw_properties = {"components", "_box_func"}
_raw_methods = {"to_pytimedelta", "sum", "std", "median", "_format_native_types"}

_delegated_properties = TimedeltaArray._datetimelike_ops + list(_raw_properties)
_delegated_methods = (
TimedeltaArray._datetimelike_methods
+ list(_raw_methods)
+ ["_box_values", "__neg__", "__pos__", "__abs__"]
)


@delegate_names(
Expand Down Expand Up @@ -225,22 +222,9 @@ def _formatter_func(self):

return _get_format_timedelta64(self, box=True)

def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
from pandas.io.formats.format import Timedelta64Formatter

return np.asarray(
Timedelta64Formatter(
values=self, nat_rep=na_rep, justify="all"
).get_result()
)

# -------------------------------------------------------------------
# Wrapping TimedeltaArray

@property
def _box_func(self):
return lambda x: Timedelta(x, unit="ns")

def __getitem__(self, key):
result = self._data.__getitem__(key)
if is_scalar(result):
Expand Down