94
94
)
95
95
from pandas .core .dtypes .generic import (
96
96
ABCDataFrame ,
97
- ABCDatetimeIndex ,
98
97
ABCIndexClass ,
99
98
ABCMultiIndex ,
100
- ABCPeriodIndex ,
101
99
ABCSeries ,
102
100
)
103
101
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"):
8246
8244
8247
8245
return result
8248
8246
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" :
8250
8250
"""
8251
8251
Cast to DatetimeIndex of timestamps, at *beginning* of period.
8252
8252
@@ -8266,23 +8266,16 @@ def to_timestamp(self, freq=None, how="start", axis=0, copy=True) -> "DataFrame"
8266
8266
-------
8267
8267
DataFrame with DatetimeIndex
8268
8268
"""
8269
- new_data = self ._data
8270
- if copy :
8271
- new_data = new_data .copy ()
8269
+ new_obj = self .copy (deep = copy )
8272
8270
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 )
8282
8274
8283
- return self ._constructor (new_data )
8275
+ setattr (new_obj , axis_name , new_ax )
8276
+ return new_obj
8284
8277
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" :
8286
8279
"""
8287
8280
Convert DataFrame from DatetimeIndex to PeriodIndex.
8288
8281
@@ -8300,23 +8293,16 @@ def to_period(self, freq=None, axis=0, copy=True) -> "DataFrame":
8300
8293
8301
8294
Returns
8302
8295
-------
8303
- TimeSeries with PeriodIndex
8296
+ DataFrame with PeriodIndex
8304
8297
"""
8305
- new_data = self ._data
8306
- if copy :
8307
- new_data = new_data .copy ()
8298
+ new_obj = self .copy (deep = copy )
8308
8299
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 )
8318
8303
8319
- return self ._constructor (new_data )
8304
+ setattr (new_obj , axis_name , new_ax )
8305
+ return new_obj
8320
8306
8321
8307
def isin (self , values ) -> "DataFrame" :
8322
8308
"""
0 commit comments