Skip to content

ENH: 2D compat for DTA tz_localize, to_period #37950

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 5 commits into from
Dec 17, 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
21 changes: 21 additions & 0 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from functools import wraps
from typing import Any, Optional, Sequence, Type, TypeVar, Union

import numpy as np
Expand Down Expand Up @@ -27,6 +28,26 @@
)


def ravel_compat(meth):
"""
Decorator to ravel a 2D array before passing it to a cython operation,
then reshape the result to our own shape.
"""

@wraps(meth)
def method(self, *args, **kwargs):
if self.ndim == 1:
return meth(self, *args, **kwargs)

flags = self._ndarray.flags
flat = self.ravel("K")
result = meth(flat, *args, **kwargs)
order = "F" if flags.f_contiguous else "C"
return result.reshape(self.shape, order=order)

return method


class NDArrayBackedExtensionArray(ExtensionArray):
"""
ExtensionArray that is backed by a single NumPy ndarray.
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
from pandas.core import nanops, ops
from pandas.core.algorithms import checked_add_with_arr, isin, unique1d, value_counts
from pandas.core.arraylike import OpsMixin
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray, ravel_compat
import pandas.core.common as com
from pandas.core.construction import array, extract_array
from pandas.core.indexers import check_array_indexer, check_setitem_lengths
Expand Down Expand Up @@ -679,6 +679,9 @@ def value_counts(self, dropna: bool = False):
-------
Series
"""
if self.ndim != 1:
raise NotImplementedError

from pandas import Index, Series

if dropna:
Expand All @@ -694,6 +697,7 @@ def value_counts(self, dropna: bool = False):
)
return Series(result._values, index=index, name=result.name)

@ravel_compat
def map(self, mapper):
# TODO(GH-23179): Add ExtensionArray.map
# Need to figure out if we want ExtensionArray.map first.
Expand Down Expand Up @@ -820,6 +824,9 @@ def freq(self, value):
value = to_offset(value)
self._validate_frequency(self, value)

if self.ndim > 1:
raise ValueError("Cannot set freq with ndim > 1")

self._freq = value

@property
Expand Down Expand Up @@ -918,7 +925,7 @@ def _is_monotonic_decreasing(self) -> bool:

@property
def _is_unique(self) -> bool:
return len(unique1d(self.asi8)) == len(self)
return len(unique1d(self.asi8.ravel("K"))) == self.size

# ------------------------------------------------------------------
# Arithmetic Methods
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,15 @@ def astype(self, dtype, copy=True):
# -----------------------------------------------------------------
# Rendering Methods

@dtl.ravel_compat
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 tslib.format_array_from_datetime(
self.asi8.ravel(), tz=self.tz, format=fmt, na_rep=na_rep
).reshape(self.shape)
self.asi8, tz=self.tz, format=fmt, na_rep=na_rep
)

# -----------------------------------------------------------------
# Comparison Methods
Expand Down Expand Up @@ -819,6 +820,7 @@ def tz_convert(self, tz):
dtype = tz_to_dtype(tz)
return self._simple_new(self.asi8, dtype=dtype, freq=self.freq)

@dtl.ravel_compat
def tz_localize(self, tz, ambiguous="raise", nonexistent="raise"):
"""
Localize tz-naive Datetime Array/Index to tz-aware
Expand Down Expand Up @@ -1051,6 +1053,7 @@ def normalize(self):
new_values = normalize_i8_timestamps(self.asi8, self.tz)
return type(self)(new_values)._with_freq("infer").tz_localize(self.tz)

@dtl.ravel_compat
def to_period(self, freq=None):
"""
Cast to PeriodArray/Index at a particular frequency.
Expand Down
1 change: 1 addition & 0 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ def _formatter(self, boxed: bool = False):
return str
return "'{}'".format

@dtl.ravel_compat
def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
"""
actually format my specific types
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,12 @@ def _formatter(self, boxed=False):

return get_format_timedelta64(self, box=True)

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

formatter = get_format_timedelta64(self._data, na_rep)
return np.array([formatter(x) for x in self._data.ravel()]).reshape(self.shape)
return np.array([formatter(x) for x in self._data])

# ----------------------------------------------------------------
# Arithmetic Methods
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,15 @@ def test_to_period(self, datetime_index, freqstr):
# an EA-specific tm.assert_ function
tm.assert_index_equal(pd.Index(result), pd.Index(expected))

def test_to_period_2d(self, arr1d):
arr2d = arr1d.reshape(1, -1)

warn = None if arr1d.tz is None else UserWarning
with tm.assert_produces_warning(warn):
result = arr2d.to_period("D")
expected = arr1d.to_period("D").reshape(1, -1)
tm.assert_period_array_equal(result, expected)

@pytest.mark.parametrize("propname", pd.DatetimeIndex._bool_ops)
def test_bool_properties(self, arr1d, propname):
# in this case _bool_ops is just `is_leap_year`
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ def test_shift_requires_tzmatch(self):
with pytest.raises(ValueError, match=msg):
dta.shift(1, fill_value=fill_value)

def test_tz_localize_t2d(self):
dti = pd.date_range("1994-05-12", periods=12, tz="US/Pacific")
dta = dti._data.reshape(3, 4)
result = dta.tz_localize(None)

expected = dta.ravel().tz_localize(None).reshape(dta.shape)
tm.assert_datetime_array_equal(result, expected)

roundtrip = expected.tz_localize("US/Pacific")
tm.assert_datetime_array_equal(roundtrip, dta)


class TestSequenceToDT64NS:
def test_tz_dtype_mismatch_raises(self):
Expand Down