Skip to content

REF: add Resolution to tslibs.__init__ #34518

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

Merged
merged 2 commits into from
Jun 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: 2 additions & 0 deletions pandas/_libs/tslibs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"OutOfBoundsDatetime",
"IncompatibleFrequency",
"Period",
"Resolution",
"Timedelta",
"delta_to_nanoseconds",
"ints_to_pytimedelta",
Expand All @@ -20,6 +21,7 @@
from .nattype import NaT, NaTType, iNaT, is_null_datetimelike, nat_strings
from .np_datetime import OutOfBoundsDatetime
from .period import IncompatibleFrequency, Period
from .resolution import Resolution
from .timedeltas import Timedelta, delta_to_nanoseconds, ints_to_pytimedelta
from .timestamps import Timestamp
from .tzconversion import tz_convert_single
19 changes: 13 additions & 6 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@

import numpy as np

from pandas._libs import NaT, NaTType, Period, Timestamp, algos, iNaT, lib
from pandas._libs.tslibs.resolution import Resolution
from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds
from pandas._libs import algos, lib
from pandas._libs.tslibs import (
NaT,
NaTType,
Period,
Resolution,
Timestamp,
delta_to_nanoseconds,
iNaT,
)
from pandas._libs.tslibs.timestamps import (
RoundTo,
integer_op_not_supported,
Expand Down Expand Up @@ -1103,7 +1110,7 @@ def inferred_freq(self):
return None

@property # NB: override with cache_readonly in immutable subclasses
def _resolution(self) -> Optional[Resolution]:
def _resolution_obj(self) -> Optional[Resolution]:
try:
return Resolution.get_reso_from_freq(self.freqstr)
except KeyError:
Expand All @@ -1114,12 +1121,12 @@ def resolution(self) -> str:
"""
Returns day, hour, minute, second, millisecond or microsecond
"""
if self._resolution is None:
if self._resolution_obj is None:
if is_period_dtype(self.dtype):
# somewhere in the past it was decided we default to day
return "day"
# otherwise we fall through and will raise
return Resolution.get_str(self._resolution)
return Resolution.get_str(self._resolution_obj)

@classmethod
def _validate_frequency(cls, index, freq, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ def is_normalized(self):
return conversion.is_date_array_normalized(self.asi8, self.tz)

@property # NB: override with cache_readonly in immutable subclasses
def _resolution(self) -> libresolution.Resolution:
def _resolution_obj(self) -> libresolution.Resolution:
return libresolution.get_resolution(self.asi8, self.tz)

# ----------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np

from pandas._libs import NaT, Timedelta, iNaT, join as libjoin, lib
from pandas._libs.tslibs import timezones
from pandas._libs.tslibs import Resolution, timezones
from pandas._libs.tslibs.parsing import DateParseError
from pandas._typing import Label
from pandas.compat.numpy import function as nv
Expand Down Expand Up @@ -78,7 +78,7 @@ def wrapper(left, right):


@inherit_names(
["inferred_freq", "_isnan", "_resolution", "resolution"],
["inferred_freq", "_isnan", "_resolution_obj", "resolution"],
DatetimeLikeArrayMixin,
cache=True,
)
Expand All @@ -93,7 +93,7 @@ class DatetimeIndexOpsMixin(ExtensionIndex):
_data: Union[DatetimeArray, TimedeltaArray, PeriodArray]
freq: Optional[DateOffset]
freqstr: Optional[str]
_resolution: int
_resolution_obj: Resolution
_bool_ops: List[str] = []
_field_ops: List[str] = []

Expand Down
8 changes: 5 additions & 3 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np

from pandas._libs import NaT, Period, Timestamp, index as libindex, lib, tslib
from pandas._libs.tslibs import fields, parsing, resolution as libresolution, timezones
from pandas._libs.tslibs import Resolution, fields, parsing, timezones
from pandas._libs.tslibs.frequencies import get_freq_group
from pandas._libs.tslibs.offsets import prefix_mapping
from pandas._typing import DtypeObj, Label
Expand Down Expand Up @@ -72,7 +72,9 @@ def _new_DatetimeIndex(cls, d):
DatetimeArray,
wrap=True,
)
@inherit_names(["_timezone", "is_normalized", "_resolution"], DatetimeArray, cache=True)
@inherit_names(
["_timezone", "is_normalized", "_resolution_obj"], DatetimeArray, cache=True
)
@inherit_names(
[
"_bool_ops",
Expand Down Expand Up @@ -525,7 +527,7 @@ def _validate_partial_date_slice(self, reso: str):
if (
self.is_monotonic
and reso in ["day", "hour", "minute", "second"]
and self._resolution >= libresolution.Resolution.from_attrname(reso)
and self._resolution_obj >= Resolution.from_attrname(reso)
):
# These resolution/monotonicity validations came from GH3931,
# GH3452 and GH2369.
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/tslibs/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_namespace():
"OutOfBoundsDatetime",
"Period",
"IncompatibleFrequency",
"Resolution",
"Timedelta",
"Timestamp",
"delta_to_nanoseconds",
Expand Down