Skip to content

Commit b1687b8

Browse files
committed
CLN: remove need for tz_localize, tz_convert in Series, use the generic
1 parent 973e076 commit b1687b8

File tree

2 files changed

+29
-97
lines changed

2 files changed

+29
-97
lines changed

pandas/core/generic.py

+28-35
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def _setup_axes(
201201

202202
def set_axis(a, i):
203203
setattr(cls, a, lib.AxisProperty(i))
204+
cls._internal_names_set.add(a)
204205

205206
if axes_are_reversed:
206207
m = cls._AXIS_LEN - 1
@@ -392,6 +393,10 @@ def _expand_axes(self, key):
392393

393394
return new_axes
394395

396+
def set_axis(self, axis, labels):
397+
""" public verson of axis assignment """
398+
setattr(self,self._get_axis_name(axis),labels)
399+
395400
def _set_axis(self, axis, labels):
396401
self._data.set_axis(axis, labels)
397402
self._clear_item_cache()
@@ -3288,7 +3293,7 @@ def truncate(self, before=None, after=None, axis=None, copy=True):
32883293

32893294
def tz_convert(self, tz, axis=0, copy=True):
32903295
"""
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
32923297
will be localized to the passed time zone.
32933298
32943299
Parameters
@@ -3304,24 +3309,18 @@ def tz_convert(self, tz, axis=0, copy=True):
33043309
ax = self._get_axis(axis)
33053310

33063311
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)
33233320

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)
33253324

33263325
def tz_localize(self, tz, axis=0, copy=True, infer_dst=False):
33273326
"""
@@ -3342,24 +3341,18 @@ def tz_localize(self, tz, axis=0, copy=True, infer_dst=False):
33423341
ax = self._get_axis(axis)
33433342

33443343
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)
33613352

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)
33633356

33643357
#----------------------------------------------------------------------
33653358
# Numeric Methods

pandas/core/series.py

+1-62
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ def rank(self, method='average', na_option='keep', ascending=True,
17261726
False for ranks by high (1) to low (N)
17271727
pct : boolean, defeault False
17281728
Computes percentage rank of data
1729-
1729+
17301730
Returns
17311731
-------
17321732
ranks : Series
@@ -2323,67 +2323,6 @@ def weekday(self):
23232323
return self._constructor([d.weekday() for d in self.index],
23242324
index=self.index).__finalize__(self)
23252325

2326-
def tz_convert(self, tz, copy=True):
2327-
"""
2328-
Convert TimeSeries to target time zone
2329-
2330-
Parameters
2331-
----------
2332-
tz : string or pytz.timezone object
2333-
copy : boolean, default True
2334-
Also make a copy of the underlying data
2335-
2336-
Returns
2337-
-------
2338-
converted : TimeSeries
2339-
"""
2340-
new_index = self.index.tz_convert(tz)
2341-
2342-
new_values = self.values
2343-
if copy:
2344-
new_values = new_values.copy()
2345-
2346-
return self._constructor(new_values,
2347-
index=new_index).__finalize__(self)
2348-
2349-
def tz_localize(self, tz, copy=True, infer_dst=False):
2350-
"""
2351-
Localize tz-naive TimeSeries to target time zone
2352-
Entries will retain their "naive" value but will be annotated as
2353-
being relative to the specified tz.
2354-
2355-
After localizing the TimeSeries, you may use tz_convert() to
2356-
get the Datetime values recomputed to a different tz.
2357-
2358-
Parameters
2359-
----------
2360-
tz : string or pytz.timezone object
2361-
copy : boolean, default True
2362-
Also make a copy of the underlying data
2363-
infer_dst : boolean, default False
2364-
Attempt to infer fall dst-transition hours based on order
2365-
2366-
Returns
2367-
-------
2368-
localized : TimeSeries
2369-
"""
2370-
from pandas.tseries.index import DatetimeIndex
2371-
2372-
if not isinstance(self.index, DatetimeIndex):
2373-
if len(self.index) > 0:
2374-
raise Exception('Cannot tz-localize non-time series')
2375-
2376-
new_index = DatetimeIndex([], tz=tz)
2377-
else:
2378-
new_index = self.index.tz_localize(tz, infer_dst=infer_dst)
2379-
2380-
new_values = self.values
2381-
if copy:
2382-
new_values = new_values.copy()
2383-
2384-
return self._constructor(new_values,
2385-
index=new_index).__finalize__(self)
2386-
23872326
@cache_readonly
23882327
def str(self):
23892328
from pandas.core.strings import StringMethods

0 commit comments

Comments
 (0)