Skip to content

Commit 5491fb6

Browse files
committed
DEPR: rename Timestamp.offset to .freq
1 parent 1edc1df commit 5491fb6

19 files changed

+140
-90
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ Deprecations
439439
- ``buffer_lines`` has been deprecated in ``pd.read_csv()`` and will be removed in a future version (:issue:`13360`)
440440
- ``as_recarray`` has been deprecated in ``pd.read_csv()`` and will be removed in a future version (:issue:`13373`)
441441
- top-level ``pd.ordered_merge()`` has been renamed to ``pd.merge_ordered()`` and the original name will be removed in a future version (:issue:`13358`)
442+
- ``Timestamp`` ``offset`` option and property has been deprecated in favor of ``freq`` (:issue:`12160`)
442443

443444
.. _whatsnew_0190.performance:
444445

pandas/io/packers.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,12 @@ def encode(obj):
481481
tz = obj.tzinfo
482482
if tz is not None:
483483
tz = u(tz.zone)
484-
offset = obj.offset
485-
if offset is not None:
486-
offset = u(offset.freqstr)
484+
freq = obj.freq
485+
if freq is not None:
486+
freq = u(freq.freqstr)
487487
return {u'typ': u'timestamp',
488488
u'value': obj.value,
489-
u'offset': offset,
489+
u'freq': freq,
490490
u'tz': tz}
491491
if isinstance(obj, NaTType):
492492
return {u'typ': u'nat'}
@@ -556,7 +556,8 @@ def decode(obj):
556556
if typ is None:
557557
return obj
558558
elif typ == u'timestamp':
559-
return Timestamp(obj[u'value'], tz=obj[u'tz'], offset=obj[u'offset'])
559+
freq = obj[u'freq'] if 'freq' in obj else obj[u'offset']
560+
return Timestamp(obj[u'value'], tz=obj[u'tz'], freq=freq)
560561
elif typ == u'nat':
561562
return NaT
562563
elif typ == u'period':
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

pandas/io/tests/generate_legacy_storage_files.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
SparseSeries, SparseDataFrame,
66
Index, MultiIndex, bdate_range, to_msgpack,
77
date_range, period_range,
8-
Timestamp, Categorical, Period)
8+
Timestamp, NaT, Categorical, Period)
99
from pandas.compat import u
1010
import os
1111
import sys
@@ -140,6 +140,13 @@ def create_data():
140140
int16=Categorical(np.arange(1000)),
141141
int32=Categorical(np.arange(10000)))
142142

143+
timestamp = dict(normal=Timestamp('2011-01-01'),
144+
nat=NaT,
145+
tz=Timestamp('2011-01-01', tz='US/Eastern'),
146+
freq=Timestamp('2011-01-01', offset='D'),
147+
both=Timestamp('2011-01-01', tz='Asia/Tokyo',
148+
offset='M'))
149+
143150
return dict(series=series,
144151
frame=frame,
145152
panel=panel,
@@ -149,7 +156,8 @@ def create_data():
149156
sp_series=dict(float=_create_sp_series(),
150157
ts=_create_sp_tsseries()),
151158
sp_frame=dict(float=_create_sp_frame()),
152-
cat=cat)
159+
cat=cat,
160+
timestamp=timestamp)
153161

154162

155163
def create_pickle_data():

pandas/io/tests/test_packers.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from distutils.version import LooseVersion
99

1010
from pandas import compat
11-
from pandas.compat import u
11+
from pandas.compat import u, PY3
1212
from pandas import (Series, DataFrame, Panel, MultiIndex, bdate_range,
1313
date_range, period_range, Index, Categorical)
1414
from pandas.core.common import PerformanceWarning
@@ -58,6 +58,19 @@ def check_arbitrary(a, b):
5858
assert_series_equal(a, b)
5959
elif isinstance(a, Index):
6060
assert_index_equal(a, b)
61+
elif isinstance(a, Categorical):
62+
# Temp,
63+
# Categorical.categories is changed from str to bytes in PY3
64+
# maybe the same as GH 13591
65+
if PY3 and b.categories.inferred_type == 'string':
66+
pass
67+
else:
68+
tm.assert_categorical_equal(a, b)
69+
elif a is NaT:
70+
tm.assertIs(b, NaT)
71+
elif isinstance(a, Timestamp):
72+
assert a == b
73+
assert a.freq == b.freq
6174
else:
6275
assert(a == b)
6376

@@ -815,8 +828,8 @@ def check_min_structure(self, data):
815828
for typ, v in self.minimum_structure.items():
816829
assert typ in data, '"{0}" not found in unpacked data'.format(typ)
817830
for kind in v:
818-
assert kind in data[
819-
typ], '"{0}" not found in data["{1}"]'.format(kind, typ)
831+
msg = '"{0}" not found in data["{1}"]'.format(kind, typ)
832+
assert kind in data[typ], msg
820833

821834
def compare(self, vf, version):
822835
# GH12277 encoding default used to be latin-1, now utf-8
@@ -839,8 +852,8 @@ def compare(self, vf, version):
839852

840853
# use a specific comparator
841854
# if available
842-
comparator = getattr(
843-
self, "compare_{typ}_{dt}".format(typ=typ, dt=dt), None)
855+
comp_method = "compare_{typ}_{dt}".format(typ=typ, dt=dt)
856+
comparator = getattr(self, comp_method, None)
844857
if comparator is not None:
845858
comparator(result, expected, typ, version)
846859
else:
@@ -872,9 +885,8 @@ def read_msgpacks(self, version):
872885
n = 0
873886
for f in os.listdir(pth):
874887
# GH12142 0.17 files packed in P2 can't be read in P3
875-
if (compat.PY3 and
876-
version.startswith('0.17.') and
877-
f.split('.')[-4][-1] == '2'):
888+
if (compat.PY3 and version.startswith('0.17.') and
889+
f.split('.')[-4][-1] == '2'):
878890
continue
879891
vf = os.path.join(pth, f)
880892
try:

pandas/io/tests/test_pickle.py

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def compare_element(self, result, expected, typ, version=None):
4646
if typ.startswith('sp_'):
4747
comparator = getattr(tm, "assert_%s_equal" % typ)
4848
comparator(result, expected, exact_indices=False)
49+
elif typ == 'timestamp':
50+
if expected is pd.NaT:
51+
assert result is pd.NaT
52+
else:
53+
tm.assert_equal(result, expected)
54+
tm.assert_equal(result.freq, expected.freq)
4955
else:
5056
comparator = getattr(tm, "assert_%s_equal" %
5157
typ, tm.assert_almost_equal)

pandas/lib.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# prototypes for sharing
22

33
cdef bint is_null_datetimelike(v)
4+
cpdef bint is_period(val)

pandas/src/inference.pyx

+1-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def is_bool(object obj):
3333
def is_complex(object obj):
3434
return util.is_complex_object(obj)
3535

36-
def is_period(object val):
36+
cpdef bint is_period(object val):
3737
""" Return a boolean if this is a Period object """
3838
return util.is_period_object(val)
3939

@@ -538,9 +538,6 @@ def is_time_array(ndarray[object] values):
538538
return False
539539
return True
540540

541-
def is_period(object o):
542-
from pandas import Period
543-
return isinstance(o,Period)
544541

545542
def is_period_array(ndarray[object] values):
546543
cdef Py_ssize_t i, n = len(values)

pandas/src/period.pyx

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ cimport cython
2424
from datetime cimport *
2525
cimport util
2626
cimport lib
27-
from lib cimport is_null_datetimelike
27+
from lib cimport is_null_datetimelike, is_period
2828
import lib
2929
from pandas import tslib
3030
from tslib import Timedelta, Timestamp, iNaT, NaT
@@ -484,8 +484,11 @@ def extract_freq(ndarray[object] values):
484484

485485
for i in range(n):
486486
p = values[i]
487+
487488
try:
488-
return p.freq
489+
# now Timestamp / NaT has freq attr
490+
if is_period(p):
491+
return p.freq
489492
except AttributeError:
490493
pass
491494

pandas/tests/indexing/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ def test_indexing_with_datetime_tz(self):
965965
# indexing - fast_xs
966966
df = DataFrame({'a': date_range('2014-01-01', periods=10, tz='UTC')})
967967
result = df.iloc[5]
968-
expected = Timestamp('2014-01-06 00:00:00+0000', tz='UTC', offset='D')
968+
expected = Timestamp('2014-01-06 00:00:00+0000', tz='UTC', freq='D')
969969
self.assertEqual(result, expected)
970970

971971
result = df.loc[5]

pandas/tests/series/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,10 @@ def test_constructor_with_datetime_tz(self):
426426
# indexing
427427
result = s.iloc[0]
428428
self.assertEqual(result, Timestamp('2013-01-01 00:00:00-0500',
429-
tz='US/Eastern', offset='D'))
429+
tz='US/Eastern', freq='D'))
430430
result = s[0]
431431
self.assertEqual(result, Timestamp('2013-01-01 00:00:00-0500',
432-
tz='US/Eastern', offset='D'))
432+
tz='US/Eastern', freq='D'))
433433

434434
result = s[Series([True, True, False], index=s.index)]
435435
assert_series_equal(result, s[0:2])

pandas/tests/test_multilevel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2365,7 +2365,7 @@ def test_reset_index_datetime(self):
23652365
'a': np.arange(6, dtype='int64')},
23662366
columns=['level_0', 'level_1', 'a'])
23672367
expected['level_1'] = expected['level_1'].apply(
2368-
lambda d: pd.Timestamp(d, offset='D', tz=tz))
2368+
lambda d: pd.Timestamp(d, freq='D', tz=tz))
23692369
assert_frame_equal(df.reset_index(), expected)
23702370

23712371
def test_reset_index_period(self):

pandas/tseries/index.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ def _generate(cls, start, end, periods, name, offset,
558558

559559
@property
560560
def _box_func(self):
561-
return lambda x: Timestamp(x, offset=self.offset, tz=self.tz)
561+
return lambda x: Timestamp(x, freq=self.offset, tz=self.tz)
562562

563563
def _convert_for_op(self, value):
564564
""" Convert value to be insertable to ndarray """
@@ -1199,8 +1199,9 @@ def __iter__(self):
11991199
for i in range(chunks):
12001200
start_i = i * chunksize
12011201
end_i = min((i + 1) * chunksize, l)
1202-
converted = tslib.ints_to_pydatetime(
1203-
data[start_i:end_i], tz=self.tz, offset=self.offset, box=True)
1202+
converted = tslib.ints_to_pydatetime(data[start_i:end_i],
1203+
tz=self.tz, freq=self.freq,
1204+
box=True)
12041205
for v in converted:
12051206
yield v
12061207

pandas/tseries/tests/test_base.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,11 @@ def test_minmax(self):
124124

125125
def test_numpy_minmax(self):
126126
dr = pd.date_range(start='2016-01-15', end='2016-01-20')
127-
self.assertEqual(np.min(dr), Timestamp(
128-
'2016-01-15 00:00:00', offset='D'))
129-
self.assertEqual(np.max(dr), Timestamp(
130-
'2016-01-20 00:00:00', offset='D'))
127+
128+
self.assertEqual(np.min(dr),
129+
Timestamp('2016-01-15 00:00:00', freq='D'))
130+
self.assertEqual(np.max(dr),
131+
Timestamp('2016-01-20 00:00:00', freq='D'))
131132

132133
errmsg = "the 'out' parameter is not supported"
133134
tm.assertRaisesRegexp(ValueError, errmsg, np.min, dr, out=0)
@@ -148,11 +149,11 @@ def test_round(self):
148149
elt = rng[1]
149150

150151
expected_rng = DatetimeIndex([
151-
Timestamp('2016-01-01 00:00:00', tz=tz, offset='30T'),
152-
Timestamp('2016-01-01 00:00:00', tz=tz, offset='30T'),
153-
Timestamp('2016-01-01 01:00:00', tz=tz, offset='30T'),
154-
Timestamp('2016-01-01 02:00:00', tz=tz, offset='30T'),
155-
Timestamp('2016-01-01 02:00:00', tz=tz, offset='30T'),
152+
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
153+
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
154+
Timestamp('2016-01-01 01:00:00', tz=tz, freq='30T'),
155+
Timestamp('2016-01-01 02:00:00', tz=tz, freq='30T'),
156+
Timestamp('2016-01-01 02:00:00', tz=tz, freq='30T'),
156157
])
157158
expected_elt = expected_rng[1]
158159

@@ -175,10 +176,10 @@ def test_repeat(self):
175176
freq='30Min', tz=tz)
176177

177178
expected_rng = DatetimeIndex([
178-
Timestamp('2016-01-01 00:00:00', tz=tz, offset='30T'),
179-
Timestamp('2016-01-01 00:00:00', tz=tz, offset='30T'),
180-
Timestamp('2016-01-01 00:30:00', tz=tz, offset='30T'),
181-
Timestamp('2016-01-01 00:30:00', tz=tz, offset='30T'),
179+
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
180+
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
181+
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
182+
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
182183
])
183184

184185
tm.assert_index_equal(rng.repeat(reps), expected_rng)
@@ -192,10 +193,10 @@ def test_numpy_repeat(self):
192193
freq='30Min', tz=tz)
193194

194195
expected_rng = DatetimeIndex([
195-
Timestamp('2016-01-01 00:00:00', tz=tz, offset='30T'),
196-
Timestamp('2016-01-01 00:00:00', tz=tz, offset='30T'),
197-
Timestamp('2016-01-01 00:30:00', tz=tz, offset='30T'),
198-
Timestamp('2016-01-01 00:30:00', tz=tz, offset='30T'),
196+
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
197+
Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'),
198+
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
199+
Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'),
199200
])
200201

201202
tm.assert_index_equal(np.repeat(rng, reps), expected_rng)

pandas/tseries/tests/test_tslib.py

+15
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,21 @@ def test_constructor_keyword(self):
255255
hour=1, minute=2, second=3, microsecond=999999)),
256256
repr(Timestamp('2015-11-12 01:02:03.999999')))
257257

258+
def test_constructor_offset_depr(self):
259+
# GH 12160
260+
with tm.assert_produces_warning(FutureWarning,
261+
check_stacklevel=False):
262+
ts = Timestamp('2011-01-01', offset='D')
263+
self.assertEqual(ts.freq, 'D')
264+
265+
with tm.assert_produces_warning(FutureWarning,
266+
check_stacklevel=False):
267+
self.assertEqual(ts.offset, 'D')
268+
269+
msg = "Can only specify freq or offset, not both"
270+
with tm.assertRaisesRegexp(TypeError, msg):
271+
Timestamp('2011-01-01', freq='D', offset='D')
272+
258273
def test_conversion(self):
259274
# GH 9255
260275
ts = Timestamp('2000-01-01')

0 commit comments

Comments
 (0)