From e1147c7726be6a3d3ec621512106422cee12d337 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 28 Feb 2020 19:25:33 -0800 Subject: [PATCH 1/3] REF: avoid using internals methods for to_timestamp, to_period --- pandas/core/frame.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f304fadbab871..50125a485e65c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8265,26 +8265,22 @@ def to_timestamp(self, freq=None, how="start", axis=0, copy=True) -> "DataFrame" ------- DataFrame with DatetimeIndex """ - new_data = self._data - if copy: - new_data = new_data.copy() + new_obj = self.copy(deep=copy) axis = self._get_axis_number(axis) if axis == 0: - assert isinstance(self.index, (ABCDatetimeIndex, ABCPeriodIndex)) - new_data.set_axis(1, self.index.to_timestamp(freq=freq, how=how)) + assert isinstance(self.index, ABCPeriodIndex) + new_obj.index = self.index.to_timestamp(freq=freq, how=how) elif axis == 1: - assert isinstance(self.columns, (ABCDatetimeIndex, ABCPeriodIndex)) - new_data.set_axis(0, self.columns.to_timestamp(freq=freq, how=how)) + assert isinstance(self.columns, ABCPeriodIndex) + new_obj.columns = self.columns.to_timestamp(freq=freq, how=how) else: # pragma: no cover raise AssertionError(f"Axis must be 0 or 1. Got {axis}") - return self._constructor(new_data) + return new_obj def to_period(self, freq=None, axis=0, copy=True) -> "DataFrame": """ - Convert DataFrame from DatetimeIndex to PeriodIndex. - Convert DataFrame from DatetimeIndex to PeriodIndex with desired frequency (inferred from index if not passed). @@ -8299,23 +8295,21 @@ def to_period(self, freq=None, axis=0, copy=True) -> "DataFrame": Returns ------- - TimeSeries with PeriodIndex + DataFrame with PeriodIndex """ - new_data = self._data - if copy: - new_data = new_data.copy() + new_obj = self.copy(deep=copy) axis = self._get_axis_number(axis) if axis == 0: assert isinstance(self.index, ABCDatetimeIndex) - new_data.set_axis(1, self.index.to_period(freq=freq)) + new_obj.index = self.index.to_period(freq=freq) elif axis == 1: assert isinstance(self.columns, ABCDatetimeIndex) - new_data.set_axis(0, self.columns.to_period(freq=freq)) + new_obj.columns = self.columns.to_period(freq=freq) else: # pragma: no cover raise AssertionError(f"Axis must be 0 or 1. Got {axis}") - return self._constructor(new_data) + return new_obj def isin(self, values) -> "DataFrame": """ From 90ea457b2ecf7e54221589e63f3795639f6c0fde Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 2 Mar 2020 11:48:09 -0800 Subject: [PATCH 2/3] simplify, annotate --- pandas/core/frame.py | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 91d361a758b6f..3752c210fd192 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -94,10 +94,8 @@ ) from pandas.core.dtypes.generic import ( ABCDataFrame, - ABCDatetimeIndex, ABCIndexClass, ABCMultiIndex, - ABCPeriodIndex, ABCSeries, ) from pandas.core.dtypes.missing import isna, notna @@ -8245,7 +8243,9 @@ def quantile(self, q=0.5, axis=0, numeric_only=True, interpolation="linear"): return result - def to_timestamp(self, freq=None, how="start", axis=0, copy=True) -> "DataFrame": + def to_timestamp( + self, freq=None, how: str = "start", axis: Axis = 0, copy: bool = True + ) -> "DataFrame": """ Cast to DatetimeIndex of timestamps, at *beginning* of period. @@ -8267,19 +8267,14 @@ def to_timestamp(self, freq=None, how="start", axis=0, copy=True) -> "DataFrame" """ new_obj = self.copy(deep=copy) - axis = self._get_axis_number(axis) - if axis == 0: - assert isinstance(self.index, ABCPeriodIndex) - new_obj.index = self.index.to_timestamp(freq=freq, how=how) - elif axis == 1: - assert isinstance(self.columns, ABCPeriodIndex) - new_obj.columns = self.columns.to_timestamp(freq=freq, how=how) - else: # pragma: no cover - raise AssertionError(f"Axis must be 0 or 1. Got {axis}") + axis_name = self._get_axis_name(axis) + old_ax = getattr(self, axis_name) + new_ax = old_ax.to_timestamp(freq=freq, how=how) + setattr(new_obj, axis_name, new_ax) return new_obj - def to_period(self, freq=None, axis=0, copy=True) -> "DataFrame": + def to_period(self, freq=None, axis: Axis = 0, copy: bool = True) -> "DataFrame": """ Convert DataFrame from DatetimeIndex to PeriodIndex with desired frequency (inferred from index if not passed). @@ -8299,16 +8294,11 @@ def to_period(self, freq=None, axis=0, copy=True) -> "DataFrame": """ new_obj = self.copy(deep=copy) - axis = self._get_axis_number(axis) - if axis == 0: - assert isinstance(self.index, ABCDatetimeIndex) - new_obj.index = self.index.to_period(freq=freq) - elif axis == 1: - assert isinstance(self.columns, ABCDatetimeIndex) - new_obj.columns = self.columns.to_period(freq=freq) - else: # pragma: no cover - raise AssertionError(f"Axis must be 0 or 1. Got {axis}") + axis_name = self._get_axis_name(axis) + old_ax = getattr(self, axis_name) + new_ax = old_ax.to_period(freq=freq) + setattr(new_obj, axis_name, new_ax) return new_obj def isin(self, values) -> "DataFrame": From a5a2d4a3f0576bf9516a9c7277115e9d0fd29a8c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 2 Mar 2020 11:56:59 -0800 Subject: [PATCH 3/3] revert docstring --- pandas/core/frame.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 3752c210fd192..8fe3a32fe3d39 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8276,6 +8276,8 @@ def to_timestamp( def to_period(self, freq=None, axis: Axis = 0, copy: bool = True) -> "DataFrame": """ + Convert DataFrame from DatetimeIndex to PeriodIndex. + Convert DataFrame from DatetimeIndex to PeriodIndex with desired frequency (inferred from index if not passed).