Skip to content

Commit

Permalink
CLN: remove need for tz_localize, tz_convert in Series, use the generic
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Feb 17, 2014
1 parent 973e076 commit b1687b8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 97 deletions.
63 changes: 28 additions & 35 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def _setup_axes(

def set_axis(a, i):
setattr(cls, a, lib.AxisProperty(i))
cls._internal_names_set.add(a)

if axes_are_reversed:
m = cls._AXIS_LEN - 1
Expand Down Expand Up @@ -392,6 +393,10 @@ def _expand_axes(self, key):

return new_axes

def set_axis(self, axis, labels):
""" public verson of axis assignment """
setattr(self,self._get_axis_name(axis),labels)

def _set_axis(self, axis, labels):
self._data.set_axis(axis, labels)
self._clear_item_cache()
Expand Down Expand Up @@ -3288,7 +3293,7 @@ def truncate(self, before=None, after=None, axis=None, copy=True):

def tz_convert(self, tz, axis=0, copy=True):
"""
Convert TimeSeries to target time zone. If it is time zone naive, it
Convert the axis to target time zone. If it is time zone naive, it
will be localized to the passed time zone.
Parameters
Expand All @@ -3304,24 +3309,18 @@ def tz_convert(self, tz, axis=0, copy=True):
ax = self._get_axis(axis)

if not hasattr(ax, 'tz_convert'):
ax_name = self._get_axis_name(axis)
raise TypeError('%s is not a valid DatetimeIndex or PeriodIndex' %
ax_name)

new_data = self._data
if copy:
new_data = new_data.copy()

new_obj = self._constructor(new_data)
new_ax = ax.tz_convert(tz)

if axis == 0:
new_obj._set_axis(1, new_ax)
elif axis == 1:
new_obj._set_axis(0, new_ax)
self._clear_item_cache()
if len(ax) > 0:
ax_name = self._get_axis_name(axis)
raise TypeError('%s is not a valid DatetimeIndex or PeriodIndex' %
ax_name)
else:
ax = DatetimeIndex([],tz=tz)
else:
ax = ax.tz_convert(tz)

return new_obj.__finalize__(self)
result = self._constructor(self._data, copy=copy)
result.set_axis(axis,ax)
return result.__finalize__(self)

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

if not hasattr(ax, 'tz_localize'):
ax_name = self._get_axis_name(axis)
raise TypeError('%s is not a valid DatetimeIndex or PeriodIndex' %
ax_name)

new_data = self._data
if copy:
new_data = new_data.copy()

new_obj = self._constructor(new_data)
new_ax = ax.tz_localize(tz, infer_dst=infer_dst)

if axis == 0:
new_obj._set_axis(1, new_ax)
elif axis == 1:
new_obj._set_axis(0, new_ax)
self._clear_item_cache()
if len(ax) > 0:
ax_name = self._get_axis_name(axis)
raise TypeError('%s is not a valid DatetimeIndex or PeriodIndex' %
ax_name)
else:
ax = DatetimeIndex([],tz=tz)
else:
ax = ax.tz_localize(tz, infer_dst=infer_dst)

return new_obj.__finalize__(self)
result = self._constructor(self._data, copy=copy)
result.set_axis(axis,ax)
return result.__finalize__(self)

#----------------------------------------------------------------------
# Numeric Methods
Expand Down
63 changes: 1 addition & 62 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,7 @@ def rank(self, method='average', na_option='keep', ascending=True,
False for ranks by high (1) to low (N)
pct : boolean, defeault False
Computes percentage rank of data
Returns
-------
ranks : Series
Expand Down Expand Up @@ -2323,67 +2323,6 @@ def weekday(self):
return self._constructor([d.weekday() for d in self.index],
index=self.index).__finalize__(self)

def tz_convert(self, tz, copy=True):
"""
Convert TimeSeries to target time zone
Parameters
----------
tz : string or pytz.timezone object
copy : boolean, default True
Also make a copy of the underlying data
Returns
-------
converted : TimeSeries
"""
new_index = self.index.tz_convert(tz)

new_values = self.values
if copy:
new_values = new_values.copy()

return self._constructor(new_values,
index=new_index).__finalize__(self)

def tz_localize(self, tz, copy=True, infer_dst=False):
"""
Localize tz-naive TimeSeries to target time zone
Entries will retain their "naive" value but will be annotated as
being relative to the specified tz.
After localizing the TimeSeries, you may use tz_convert() to
get the Datetime values recomputed to a different tz.
Parameters
----------
tz : string or pytz.timezone object
copy : boolean, default True
Also make a copy of the underlying data
infer_dst : boolean, default False
Attempt to infer fall dst-transition hours based on order
Returns
-------
localized : TimeSeries
"""
from pandas.tseries.index import DatetimeIndex

if not isinstance(self.index, DatetimeIndex):
if len(self.index) > 0:
raise Exception('Cannot tz-localize non-time series')

new_index = DatetimeIndex([], tz=tz)
else:
new_index = self.index.tz_localize(tz, infer_dst=infer_dst)

new_values = self.values
if copy:
new_values = new_values.copy()

return self._constructor(new_values,
index=new_index).__finalize__(self)

@cache_readonly
def str(self):
from pandas.core.strings import StringMethods
Expand Down

0 comments on commit b1687b8

Please sign in to comment.