Skip to content

Commit 5d99cff

Browse files
sinhrksnateGeorge
authored andcommitted
DEPR: rename Timestamp.offset to .freq
closes pandas-dev#12160 Author: sinhrks <[email protected]> Closes pandas-dev#13593 from sinhrks/depr_timestamp_offset and squashes the following commits: c7749d5 [sinhrks] DEPR: rename Timestamp.offset to .freq
1 parent 6c8b21b commit 5d99cff

20 files changed

+187
-142
lines changed

doc/source/whatsnew/v0.19.0.txt

+14-18
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ Other enhancements
194194
pd.to_numeric(s, downcast='unsigned')
195195
pd.to_numeric(s, downcast='integer')
196196

197-
- ``Index`` now supports ``.str.extractall()`` which returns a ``DataFrame``, see :ref:`documentation here <text.extractall>` (:issue:`10008`, :issue:`13156`)
197+
- ``Index`` now supports ``.str.extractall()`` which returns a ``DataFrame``, the see :ref:`docs here <text.extractall>` (:issue:`10008`, :issue:`13156`)
198198
- ``.to_hdf/read_hdf()`` now accept path objects (e.g. ``pathlib.Path``, ``py.path.local``) for the file path (:issue:`11773`)
199199

200200
.. ipython:: python
201201

202202
idx = pd.Index(["a1a2", "b1", "c1"])
203203
idx.str.extractall("[ab](?P<digit>\d)")
204204

205-
- ``Timestamp`` s can now accept positional and keyword parameters like :func:`datetime.datetime` (:issue:`10758`, :issue:`11630`)
205+
- ``Timestamp`` can now accept positional and keyword parameters similar to :func:`datetime.datetime` (:issue:`10758`, :issue:`11630`)
206206

207207
.. ipython:: python
208208

@@ -227,8 +227,7 @@ Other enhancements
227227
- Consistent with the Python API, ``pd.read_csv()`` will now interpret ``+inf`` as positive infinity (:issue:`13274`)
228228
- The ``DataFrame`` constructor will now respect key ordering if a list of ``OrderedDict`` objects are passed in (:issue:`13304`)
229229
- ``pd.read_html()`` has gained support for the ``decimal`` option (:issue:`12907`)
230-
- A ``union_categorical`` function has been added for combining categoricals, see :ref:`Unioning Categoricals<categorical.union>` (:issue:`13361`)
231-
- ``eval``'s upcasting rules for ``float32`` types have been updated to be more consistent with NumPy's rules. New behavior will not upcast to ``float64`` if you multiply a pandas ``float32`` object by a scalar float64. (:issue:`12388`)
230+
- A top-level function :func:`union_categorical` has been added for combining categoricals, see :ref:`Unioning Categoricals<categorical.union>` (:issue:`13361`)
232231
- ``Series`` has gained the properties ``.is_monotonic``, ``.is_monotonic_increasing``, ``.is_monotonic_decreasing``, similar to ``Index`` (:issue:`13336`)
233232

234233
.. _whatsnew_0190.api:
@@ -238,9 +237,16 @@ API changes
238237

239238

240239
- Non-convertible dates in an excel date column will be returned without conversion and the column will be ``object`` dtype, rather than raising an exception (:issue:`10001`)
240+
- ``eval``'s upcasting rules for ``float32`` types have been updated to be more consistent with NumPy's rules. New behavior will not upcast to ``float64`` if you multiply a pandas ``float32`` object by a scalar float64. (:issue:`12388`)
241241
- An ``UnsupportedFunctionCall`` error is now raised if NumPy ufuncs like ``np.mean`` are called on groupby or resample objects (:issue:`12811`)
242242
- Calls to ``.sample()`` will respect the random seed set via ``numpy.random.seed(n)`` (:issue:`13161`)
243243
- ``Styler.apply`` is now more strict about the outputs your function must return. For ``axis=0`` or ``axis=1``, the output shape must be identical. For ``axis=None``, the output must be a DataFrame with identical columns and index labels. (:issue:`13222`)
244+
- ``Float64Index.astype(int)`` will now raise ``ValueError`` if ``Float64Index`` contains ``NaN`` values (:issue:`13149`)
245+
- ``TimedeltaIndex.astype(int)`` and ``DatetimeIndex.astype(int)`` will now return ``Int64Index`` instead of ``np.array`` (:issue:`13209`)
246+
- ``.filter()`` enforces mutual exclusion of the keyword arguments. (:issue:`12399`)
247+
- ``PeridIndex`` can now accept ``list`` and ``array`` which contains ``pd.NaT`` (:issue:`13430`)
248+
- ``__setitem__`` will no longer apply a callable rhs as a function instead of storing it. Call ``where`` directly to get the previous behavior. (:issue:`13299`)
249+
244250

245251
.. _whatsnew_0190.api.tolist:
246252

@@ -361,7 +367,7 @@ We are able to preserve the join keys
361367
pd.merge(df1, df2, how='outer').dtypes
362368

363369
Of course if you have missing values that are introduced, then the
364-
resulting dtype will be upcast (unchanged from previous).
370+
resulting dtype will be upcast, which is unchanged from previous.
365371

366372
.. ipython:: python
367373

@@ -419,17 +425,6 @@ Furthermore:
419425
- Passing duplicated ``percentiles`` will now raise a ``ValueError``.
420426
- Bug in ``.describe()`` on a DataFrame with a mixed-dtype column index, which would previously raise a ``TypeError`` (:issue:`13288`)
421427

422-
.. _whatsnew_0190.api.other:
423-
424-
Other API changes
425-
^^^^^^^^^^^^^^^^^
426-
427-
- ``Float64Index.astype(int)`` will now raise ``ValueError`` if ``Float64Index`` contains ``NaN`` values (:issue:`13149`)
428-
- ``TimedeltaIndex.astype(int)`` and ``DatetimeIndex.astype(int)`` will now return ``Int64Index`` instead of ``np.array`` (:issue:`13209`)
429-
- ``.filter()`` enforces mutual exclusion of the keyword arguments. (:issue:`12399`)
430-
- ``PeridIndex`` can now accept ``list`` and ``array`` which contains ``pd.NaT`` (:issue:`13430`)
431-
- ``__setitem__`` will no longer apply a callable rhs as a function instead of storing it. Call ``where`` directly to get the previous behavior. (:issue:`13299`)
432-
433428
.. _whatsnew_0190.deprecations:
434429

435430
Deprecations
@@ -439,6 +434,7 @@ Deprecations
439434
- ``buffer_lines`` has been deprecated in ``pd.read_csv()`` and will be removed in a future version (:issue:`13360`)
440435
- ``as_recarray`` has been deprecated in ``pd.read_csv()`` and will be removed in a future version (:issue:`13373`)
441436
- 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`)
437+
- ``Timestamp.offset`` property (and named arg in the constructor), has been deprecated in favor of ``freq`` (:issue:`12160`)
442438

443439
.. _whatsnew_0190.performance:
444440

@@ -503,7 +499,7 @@ Bug Fixes
503499
- Bug in ``pd.read_csv()`` in which the ``nrows`` argument was not properly validated for both engines (:issue:`10476`)
504500
- Bug in ``pd.read_csv()`` with ``engine='python'`` in which infinities of mixed-case forms were not being interpreted properly (:issue:`13274`)
505501
- Bug in ``pd.read_csv()`` with ``engine='python'`` in which trailing ``NaN`` values were not being parsed (:issue:`13320`)
506-
- Bug in ``pd.read_csv()`` with ``engine='python'`` when reading from a tempfile.TemporaryFile on Windows with Python 3 (:issue:`13398`)
502+
- Bug in ``pd.read_csv()`` with ``engine='python'`` when reading from a ``tempfile.TemporaryFile`` on Windows with Python 3 (:issue:`13398`)
507503
- Bug in ``pd.read_csv()`` that prevents ``usecols`` kwarg from accepting single-byte unicode strings (:issue:`13219`)
508504
- Bug in ``pd.read_csv()`` that prevents ``usecols`` from being an empty set (:issue:`13402`)
509505
- Bug in ``pd.read_csv()`` with ``engine=='c'`` in which null ``quotechar`` was not accepted even though ``quoting`` was specified as ``None`` (:issue:`13411`)
@@ -516,7 +512,7 @@ Bug Fixes
516512

517513

518514
- Bug in ``pd.to_datetime()`` when passing invalid datatypes (e.g. bool); will now respect the ``errors`` keyword (:issue:`13176`)
519-
- Bug in ``pd.to_datetime()`` which overflowed on ``int8``, `int16`` dtypes (:issue:`13451`)
515+
- Bug in ``pd.to_datetime()`` which overflowed on ``int8``, and ``int16`` dtypes (:issue:`13451`)
520516
- Bug in extension dtype creation where the created types were not is/identical (:issue:`13285`)
521517

522518
- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)

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', freq='D'),
147+
both=Timestamp('2011-01-01', tz='Asia/Tokyo',
148+
freq='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+
assert b is 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)

0 commit comments

Comments
 (0)