Skip to content

Commit ce86c30

Browse files
author
Chang She
committed
ENH: cleaner implementation for #1191
1 parent f02eb4f commit ce86c30

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

pandas/core/frame.py

+6-15
Original file line numberDiff line numberDiff line change
@@ -3981,18 +3981,14 @@ def to_timestamp(self, freq=None, how='start', axis=0, copy=True):
39813981
if copy:
39823982
new_data = new_data.copy()
39833983

3984-
new_index, new_columns = self.index, self.columns
3985-
39863984
if axis == 0:
3987-
new_index = self.index.to_timestamp(freq=freq, how=how)
3985+
new_data.set_axis(1, self.index.to_timestamp(freq=freq, how=how))
39883986
elif axis == 1:
3989-
new_columns = self.columns.to_timestamp(freq=freq, how=how)
3987+
new_data.set_axis(0, self.columns.to_timestamp(freq=freq, how=how))
39903988
else:
39913989
raise ValueError('Axis must be 0 or 1. Got %s' % str(axis))
39923990

3993-
axes = [new_columns, new_index]
3994-
new_data = BlockManager(new_data.blocks, axes)
3995-
return DataFrame(new_data, index=new_index, columns=new_columns)
3991+
return DataFrame(new_data)
39963992

39973993
def to_period(self, freq=None, axis=0, copy=True):
39983994
"""
@@ -4015,23 +4011,18 @@ def to_period(self, freq=None, axis=0, copy=True):
40154011
if copy:
40164012
new_data = new_data.copy()
40174013

4018-
new_index, new_columns = self.index, self.columns
4019-
40204014
if axis == 0:
40214015
if freq is None:
40224016
freq = self.index.freqstr or self.index.inferred_freq
4023-
new_index = self.index.to_period(freq=freq)
4017+
new_data.set_axis(1, self.index.to_period(freq=freq))
40244018
elif axis == 1:
40254019
if freq is None:
40264020
freq = self.columns.freqstr or self.columns.inferred_freq
4027-
new_columns = self.columns.to_period(freq=freq)
4021+
new_data.set_axis(0, self.columns.to_period(freq=freq))
40284022
else:
40294023
raise ValueError('Axis must be 0 or 1. Got %s' % str(axis))
40304024

4031-
axes = [new_columns, new_index]
4032-
new_data = BlockManager(new_data.blocks, axes)
4033-
4034-
return DataFrame(new_data, index=new_index, columns=new_columns)
4025+
return DataFrame(new_data)
40354026

40364027
#----------------------------------------------------------------------
40374028
# Deprecated stuff

0 commit comments

Comments
 (0)