@@ -201,6 +201,7 @@ def _setup_axes(
201
201
202
202
def set_axis (a , i ):
203
203
setattr (cls , a , lib .AxisProperty (i ))
204
+ cls ._internal_names_set .add (a )
204
205
205
206
if axes_are_reversed :
206
207
m = cls ._AXIS_LEN - 1
@@ -392,6 +393,10 @@ def _expand_axes(self, key):
392
393
393
394
return new_axes
394
395
396
+ def set_axis (self , axis , labels ):
397
+ """ public verson of axis assignment """
398
+ setattr (self ,self ._get_axis_name (axis ),labels )
399
+
395
400
def _set_axis (self , axis , labels ):
396
401
self ._data .set_axis (axis , labels )
397
402
self ._clear_item_cache ()
@@ -3288,7 +3293,7 @@ def truncate(self, before=None, after=None, axis=None, copy=True):
3288
3293
3289
3294
def tz_convert (self , tz , axis = 0 , copy = True ):
3290
3295
"""
3291
- Convert TimeSeries to target time zone. If it is time zone naive, it
3296
+ Convert the axis to target time zone. If it is time zone naive, it
3292
3297
will be localized to the passed time zone.
3293
3298
3294
3299
Parameters
@@ -3304,24 +3309,18 @@ def tz_convert(self, tz, axis=0, copy=True):
3304
3309
ax = self ._get_axis (axis )
3305
3310
3306
3311
if not hasattr (ax , 'tz_convert' ):
3307
- ax_name = self ._get_axis_name (axis )
3308
- raise TypeError ('%s is not a valid DatetimeIndex or PeriodIndex' %
3309
- ax_name )
3310
-
3311
- new_data = self ._data
3312
- if copy :
3313
- new_data = new_data .copy ()
3314
-
3315
- new_obj = self ._constructor (new_data )
3316
- new_ax = ax .tz_convert (tz )
3317
-
3318
- if axis == 0 :
3319
- new_obj ._set_axis (1 , new_ax )
3320
- elif axis == 1 :
3321
- new_obj ._set_axis (0 , new_ax )
3322
- self ._clear_item_cache ()
3312
+ if len (ax ) > 0 :
3313
+ ax_name = self ._get_axis_name (axis )
3314
+ raise TypeError ('%s is not a valid DatetimeIndex or PeriodIndex' %
3315
+ ax_name )
3316
+ else :
3317
+ ax = DatetimeIndex ([],tz = tz )
3318
+ else :
3319
+ ax = ax .tz_convert (tz )
3323
3320
3324
- return new_obj .__finalize__ (self )
3321
+ result = self ._constructor (self ._data , copy = copy )
3322
+ result .set_axis (axis ,ax )
3323
+ return result .__finalize__ (self )
3325
3324
3326
3325
def tz_localize (self , tz , axis = 0 , copy = True , infer_dst = False ):
3327
3326
"""
@@ -3342,24 +3341,18 @@ def tz_localize(self, tz, axis=0, copy=True, infer_dst=False):
3342
3341
ax = self ._get_axis (axis )
3343
3342
3344
3343
if not hasattr (ax , 'tz_localize' ):
3345
- ax_name = self ._get_axis_name (axis )
3346
- raise TypeError ('%s is not a valid DatetimeIndex or PeriodIndex' %
3347
- ax_name )
3348
-
3349
- new_data = self ._data
3350
- if copy :
3351
- new_data = new_data .copy ()
3352
-
3353
- new_obj = self ._constructor (new_data )
3354
- new_ax = ax .tz_localize (tz , infer_dst = infer_dst )
3355
-
3356
- if axis == 0 :
3357
- new_obj ._set_axis (1 , new_ax )
3358
- elif axis == 1 :
3359
- new_obj ._set_axis (0 , new_ax )
3360
- self ._clear_item_cache ()
3344
+ if len (ax ) > 0 :
3345
+ ax_name = self ._get_axis_name (axis )
3346
+ raise TypeError ('%s is not a valid DatetimeIndex or PeriodIndex' %
3347
+ ax_name )
3348
+ else :
3349
+ ax = DatetimeIndex ([],tz = tz )
3350
+ else :
3351
+ ax = ax .tz_localize (tz , infer_dst = infer_dst )
3361
3352
3362
- return new_obj .__finalize__ (self )
3353
+ result = self ._constructor (self ._data , copy = copy )
3354
+ result .set_axis (axis ,ax )
3355
+ return result .__finalize__ (self )
3363
3356
3364
3357
#----------------------------------------------------------------------
3365
3358
# Numeric Methods
0 commit comments