Skip to content

Commit dcd86e5

Browse files
authored
REF: avoid using internals methods for to_timestamp, to_period (#32347)
1 parent d33b002 commit dcd86e5

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

pandas/core/frame.py

+17-31
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@
9494
)
9595
from pandas.core.dtypes.generic import (
9696
ABCDataFrame,
97-
ABCDatetimeIndex,
9897
ABCIndexClass,
9998
ABCMultiIndex,
100-
ABCPeriodIndex,
10199
ABCSeries,
102100
)
103101
from pandas.core.dtypes.missing import isna, notna
@@ -8246,7 +8244,9 @@ def quantile(self, q=0.5, axis=0, numeric_only=True, interpolation="linear"):
82468244

82478245
return result
82488246

8249-
def to_timestamp(self, freq=None, how="start", axis=0, copy=True) -> "DataFrame":
8247+
def to_timestamp(
8248+
self, freq=None, how: str = "start", axis: Axis = 0, copy: bool = True
8249+
) -> "DataFrame":
82508250
"""
82518251
Cast to DatetimeIndex of timestamps, at *beginning* of period.
82528252
@@ -8266,23 +8266,16 @@ def to_timestamp(self, freq=None, how="start", axis=0, copy=True) -> "DataFrame"
82668266
-------
82678267
DataFrame with DatetimeIndex
82688268
"""
8269-
new_data = self._data
8270-
if copy:
8271-
new_data = new_data.copy()
8269+
new_obj = self.copy(deep=copy)
82728270

8273-
axis = self._get_axis_number(axis)
8274-
if axis == 0:
8275-
assert isinstance(self.index, (ABCDatetimeIndex, ABCPeriodIndex))
8276-
new_data.set_axis(1, self.index.to_timestamp(freq=freq, how=how))
8277-
elif axis == 1:
8278-
assert isinstance(self.columns, (ABCDatetimeIndex, ABCPeriodIndex))
8279-
new_data.set_axis(0, self.columns.to_timestamp(freq=freq, how=how))
8280-
else: # pragma: no cover
8281-
raise AssertionError(f"Axis must be 0 or 1. Got {axis}")
8271+
axis_name = self._get_axis_name(axis)
8272+
old_ax = getattr(self, axis_name)
8273+
new_ax = old_ax.to_timestamp(freq=freq, how=how)
82828274

8283-
return self._constructor(new_data)
8275+
setattr(new_obj, axis_name, new_ax)
8276+
return new_obj
82848277

8285-
def to_period(self, freq=None, axis=0, copy=True) -> "DataFrame":
8278+
def to_period(self, freq=None, axis: Axis = 0, copy: bool = True) -> "DataFrame":
82868279
"""
82878280
Convert DataFrame from DatetimeIndex to PeriodIndex.
82888281
@@ -8300,23 +8293,16 @@ def to_period(self, freq=None, axis=0, copy=True) -> "DataFrame":
83008293
83018294
Returns
83028295
-------
8303-
TimeSeries with PeriodIndex
8296+
DataFrame with PeriodIndex
83048297
"""
8305-
new_data = self._data
8306-
if copy:
8307-
new_data = new_data.copy()
8298+
new_obj = self.copy(deep=copy)
83088299

8309-
axis = self._get_axis_number(axis)
8310-
if axis == 0:
8311-
assert isinstance(self.index, ABCDatetimeIndex)
8312-
new_data.set_axis(1, self.index.to_period(freq=freq))
8313-
elif axis == 1:
8314-
assert isinstance(self.columns, ABCDatetimeIndex)
8315-
new_data.set_axis(0, self.columns.to_period(freq=freq))
8316-
else: # pragma: no cover
8317-
raise AssertionError(f"Axis must be 0 or 1. Got {axis}")
8300+
axis_name = self._get_axis_name(axis)
8301+
old_ax = getattr(self, axis_name)
8302+
new_ax = old_ax.to_period(freq=freq)
83188303

8319-
return self._constructor(new_data)
8304+
setattr(new_obj, axis_name, new_ax)
8305+
return new_obj
83208306

83218307
def isin(self, values) -> "DataFrame":
83228308
"""

0 commit comments

Comments
 (0)