Skip to content

Commit 3f3839b

Browse files
gfyoungjorisvandenbossche
authored andcommitted
DEPR: Deprecate pandas.core.datetools (#14105)
* MAINT: Replace datetools import in tests * MAINT: Replace datetools import internally * DOC: Replace datetools import in docs * MAINT: Remove datetool imports from scripts * DEPR: Deprecate pandas.core.datetools Closes gh-14094.
1 parent e88ad28 commit 3f3839b

27 files changed

+356
-231
lines changed

doc/source/timeseries.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from datetime import datetime, timedelta, time
88
import numpy as np
99
import pandas as pd
10-
from pandas import datetools
10+
from pandas import offsets
1111
np.random.seed(123456)
1212
randn = np.random.randn
1313
randint = np.random.randint
@@ -1223,7 +1223,7 @@ The shift method accepts an ``freq`` argument which can accept a
12231223

12241224
.. ipython:: python
12251225
1226-
ts.shift(5, freq=datetools.bday)
1226+
ts.shift(5, freq=offsets.BDay())
12271227
ts.shift(5, freq='BM')
12281228
12291229
Rather than changing the alignment of the data and the index, ``DataFrame`` and
@@ -1246,7 +1246,7 @@ around ``reindex`` which generates a ``date_range`` and calls ``reindex``.
12461246

12471247
.. ipython:: python
12481248
1249-
dr = pd.date_range('1/1/2010', periods=3, freq=3 * datetools.bday)
1249+
dr = pd.date_range('1/1/2010', periods=3, freq=3 * offsets.BDay())
12501250
ts = pd.Series(randn(3), index=dr)
12511251
ts
12521252
ts.asfreq(BDay())

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,7 @@ Deprecations
12951295

12961296
- ``PeriodIndex.to_datetime`` has been deprecated in favour of ``PeriodIndex.to_timestamp`` (:issue:`8254`)
12971297
- ``Timestamp.to_datetime`` has been deprecated in favour of ``Timestamp.to_pydatetime`` (:issue:`8254`)
1298+
- ``pandas.core.datetools`` module has been deprecated and will be removed in a subsequent release (:issue:`14094`)
12981299
- ``Index.to_datetime`` and ``DatetimeIndex.to_datetime`` have been deprecated in favour of ``pd.to_datetime`` (:issue:`8254`)
12991300
- ``SparseList`` has been deprecated and will be removed in a future version (:issue:`13784`)
13001301
- ``DataFrame.to_html()`` and ``DataFrame.to_latex()`` have dropped the ``colSpace`` parameter in favor of ``col_space`` (:issue:`13857`)

pandas/api/tests/test_api.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class TestPDApi(Base, tm.TestCase):
4242
'json', 'lib', 'index', 'parser']
4343

4444
# these are already deprecated; awaiting removal
45-
deprecated_modules = ['ols', 'stats']
45+
deprecated_modules = ['ols', 'stats', 'datetools']
4646

4747
# misc
4848
misc = ['IndexSlice', 'NaT']
@@ -61,14 +61,14 @@ class TestPDApi(Base, tm.TestCase):
6161
'SparseTimeSeries', 'Panel4D',
6262
'SparseList']
6363

64-
# these should be deperecated in the future
64+
# these should be deprecated in the future
6565
deprecated_classes_in_future = ['Term', 'Panel']
6666

6767
# these should be removed from top-level namespace
6868
remove_classes_from_top_level_namespace = ['Expr']
6969

7070
# external modules exposed in pandas namespace
71-
modules = ['np', 'datetime', 'datetools']
71+
modules = ['np', 'datetime']
7272

7373
# top-level functions
7474
funcs = ['bdate_range', 'concat', 'crosstab', 'cut',
@@ -99,7 +99,7 @@ class TestPDApi(Base, tm.TestCase):
9999
funcs_to = ['to_datetime', 'to_msgpack',
100100
'to_numeric', 'to_pickle', 'to_timedelta']
101101

102-
# these should be deperecated in the future
102+
# these should be deprecated in the future
103103
deprecated_funcs_in_future = ['pnow', 'groupby', 'info']
104104

105105
# these are already deprecated; awaiting removal
@@ -208,6 +208,19 @@ def test_removed_from_core_common(self):
208208
'ensure_float']:
209209
self.assertRaises(AttributeError, lambda: getattr(com, t))
210210

211+
212+
class TestDatetools(tm.TestCase):
213+
214+
def test_deprecation_access_func(self):
215+
with tm.assert_produces_warning(FutureWarning,
216+
check_stacklevel=False):
217+
pd.datetools.to_datetime('2016-01-01')
218+
219+
def test_deprecation_access_obj(self):
220+
with tm.assert_produces_warning(FutureWarning,
221+
check_stacklevel=False):
222+
pd.datetools.monthEnd
223+
211224
if __name__ == '__main__':
212225
import nose
213226
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

pandas/core/api.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,18 @@
2828
from pandas.tseries.tdi import TimedeltaIndex, Timedelta
2929
from pandas.tseries.period import Period, PeriodIndex
3030

31-
# legacy
32-
import pandas.core.datetools as datetools
31+
# see gh-14094.
32+
from pandas.util.depr_module import _DeprecatedModule
33+
34+
_alts = ['pandas.tseries.tools', 'pandas.tseries.offsets',
35+
'pandas.tseries.frequencies']
36+
_removals = ['day', 'bday', 'businessDay', 'cday', 'customBusinessDay',
37+
'customBusinessMonthEnd', 'customBusinessMonthBegin',
38+
'monthEnd', 'yearEnd', 'yearBegin', 'bmonthEnd', 'bmonthBegin',
39+
'cbmonthEnd', 'cbmonthBegin', 'bquarterEnd', 'quarterEnd',
40+
'byearEnd', 'week']
41+
datetools = _DeprecatedModule(deprmod='pandas.core.datetools', alts=_alts,
42+
removals=_removals)
3343

3444
from pandas.core.config import (get_option, set_option, reset_option,
3545
describe_option, option_context, options)

pandas/core/datetools.py

+6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
# flake8: noqa
44

5+
import warnings
6+
57
from pandas.tseries.tools import *
68
from pandas.tseries.offsets import *
79
from pandas.tseries.frequencies import *
810

11+
warnings.warn("The pandas.core.datetools module is deprecated and will be "
12+
"removed in a future version. Please use the pandas.tseries "
13+
"module instead.", FutureWarning, stacklevel=2)
14+
915
day = DateOffset()
1016
bday = BDay()
1117
businessDay = bday

pandas/core/generic.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
import pandas.core.algorithms as algos
4343
import pandas.core.common as com
4444
import pandas.core.missing as missing
45-
import pandas.core.datetools as datetools
4645
from pandas.formats.printing import pprint_thing
4746
from pandas.formats.format import format_percentiles
47+
from pandas.tseries.frequencies import to_offset
4848
from pandas import compat
4949
from pandas.compat.numpy import function as nv
5050
from pandas.compat import (map, zip, lrange, string_types,
@@ -4792,7 +4792,7 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
47924792
periods : int
47934793
Number of periods to move, can be positive or negative
47944794
freq : DateOffset, timedelta, or time rule string, optional
4795-
Increment to use from datetools module or time rule (e.g. 'EOM').
4795+
Increment to use from the tseries module or time rule (e.g. 'EOM').
47964796
See Notes.
47974797
axis : %(axes_single_arg)s
47984798
@@ -4865,7 +4865,7 @@ def tshift(self, periods=1, freq=None, axis=0):
48654865
periods : int
48664866
Number of periods to move, can be positive or negative
48674867
freq : DateOffset, timedelta, or time rule string, default None
4868-
Increment to use from datetools module or time rule (e.g. 'EOM')
4868+
Increment to use from the tseries module or time rule (e.g. 'EOM')
48694869
axis : int or basestring
48704870
Corresponds to the axis that contains the Index
48714871
@@ -4895,11 +4895,11 @@ def tshift(self, periods=1, freq=None, axis=0):
48954895
return self
48964896

48974897
if isinstance(freq, string_types):
4898-
freq = datetools.to_offset(freq)
4898+
freq = to_offset(freq)
48994899

49004900
block_axis = self._get_block_manager_axis(axis)
49014901
if isinstance(index, PeriodIndex):
4902-
orig_freq = datetools.to_offset(index.freq)
4902+
orig_freq = to_offset(index.freq)
49034903
if freq == orig_freq:
49044904
new_data = self._data.copy()
49054905
new_data.axes[block_axis] = index.shift(periods)

pandas/io/tests/test_sql.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from pandas import date_range, to_datetime, to_timedelta, Timestamp
3838
import pandas.compat as compat
3939
from pandas.compat import StringIO, range, lrange, string_types
40-
from pandas.core.datetools import format as date_format
40+
from pandas.tseries.tools import format as date_format
4141

4242
import pandas.io.sql as sql
4343
from pandas.io.sql import read_sql_table, read_sql_query

pandas/sparse/tests/test_frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from pandas import Series, DataFrame, bdate_range, Panel
1010
from pandas.tseries.index import DatetimeIndex
11-
import pandas.core.datetools as datetools
11+
from pandas.tseries.offsets import BDay
1212
import pandas.util.testing as tm
1313
from pandas.compat import lrange
1414
from pandas import compat
@@ -850,8 +850,8 @@ def _check(frame, orig):
850850
exp = exp.to_sparse(frame.default_fill_value)
851851
tm.assert_frame_equal(shifted, exp)
852852

853-
shifted = frame.shift(2, freq=datetools.bday)
854-
exp = orig.shift(2, freq=datetools.bday)
853+
shifted = frame.shift(2, freq=BDay())
854+
exp = orig.shift(2, freq=BDay())
855855
exp = exp.to_sparse(frame.default_fill_value)
856856
tm.assert_frame_equal(shifted, exp)
857857

pandas/sparse/tests/test_series.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import pandas as pd
88

99
from pandas import Series, DataFrame, bdate_range
10-
from pandas.core.datetools import BDay
11-
import pandas.core.datetools as datetools
1210
from pandas.core.common import isnull
11+
from pandas.tseries.offsets import BDay
1312
import pandas.util.testing as tm
1413
from pandas.compat import range
1514
from pandas import compat
@@ -843,7 +842,7 @@ def test_shift(self):
843842
f = lambda s: s.shift(2, freq='B')
844843
_dense_series_compare(series, f)
845844

846-
f = lambda s: s.shift(2, freq=datetools.bday)
845+
f = lambda s: s.shift(2, freq=BDay())
847846
_dense_series_compare(series, f)
848847

849848
def test_shift_nan(self):

pandas/stats/tests/test_ols.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
from pandas import date_range, bdate_range
1818
from pandas.core.panel import Panel
19-
from pandas import DataFrame, Index, Series, notnull, datetools
19+
from pandas import DataFrame, Index, Series, notnull, offsets
2020
from pandas.stats.api import ols
2121
from pandas.stats.ols import _filter_data
2222
from pandas.stats.plm import NonPooledPanelOLS, PanelOLS
2323
from pandas.util.testing import (assert_almost_equal, assert_series_equal,
2424
assert_frame_equal, assertRaisesRegexp, slow)
2525
import pandas.util.testing as tm
2626
import pandas.compat as compat
27-
from .common import BaseTest
27+
from pandas.stats.tests.common import BaseTest
2828

2929
_have_statsmodels = True
3030
try:
@@ -898,22 +898,22 @@ class TestOLSFilter(tm.TestCase):
898898

899899
def setUp(self):
900900
date_index = date_range(datetime(2009, 12, 11), periods=3,
901-
freq=datetools.bday)
901+
freq=offsets.BDay())
902902
ts = Series([3, 1, 4], index=date_index)
903903
self.TS1 = ts
904904

905905
date_index = date_range(datetime(2009, 12, 11), periods=5,
906-
freq=datetools.bday)
906+
freq=offsets.BDay())
907907
ts = Series([1, 5, 9, 2, 6], index=date_index)
908908
self.TS2 = ts
909909

910910
date_index = date_range(datetime(2009, 12, 11), periods=3,
911-
freq=datetools.bday)
911+
freq=offsets.BDay())
912912
ts = Series([5, np.nan, 3], index=date_index)
913913
self.TS3 = ts
914914

915915
date_index = date_range(datetime(2009, 12, 11), periods=5,
916-
freq=datetools.bday)
916+
freq=offsets.BDay())
917917
ts = Series([np.nan, 5, 8, 9, 7], index=date_index)
918918
self.TS4 = ts
919919

pandas/tests/frame/test_indexing.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
date_range)
1818
import pandas as pd
1919

20+
from pandas.tseries.offsets import BDay
2021
from pandas.types.common import (is_float_dtype,
2122
is_integer,
2223
is_scalar)
@@ -2068,8 +2069,6 @@ def test_at_time_between_time_datetimeindex(self):
20682069
assert_frame_equal(result, df)
20692070

20702071
def test_xs(self):
2071-
from pandas.core.datetools import bday
2072-
20732072
idx = self.frame.index[5]
20742073
xs = self.frame.xs(idx)
20752074
for item, value in compat.iteritems(xs):
@@ -2090,7 +2089,7 @@ def test_xs(self):
20902089
self.assertEqual(xs['B'], '1')
20912090

20922091
with tm.assertRaises(KeyError):
2093-
self.tsframe.xs(self.tsframe.index[0] - bday)
2092+
self.tsframe.xs(self.tsframe.index[0] - BDay())
20942093

20952094
# xs get column
20962095
series = self.frame.xs('A', axis=1)
@@ -2772,3 +2771,9 @@ def test_transpose(self):
27722771
expected = DataFrame(self.df.values.T)
27732772
expected.index = ['A', 'B']
27742773
assert_frame_equal(result, expected)
2774+
2775+
2776+
if __name__ == '__main__':
2777+
import nose
2778+
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
2779+
exit=False)

pandas/tests/frame/test_timeseries.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from pandas import DataFrame, Series, Index, Timestamp, DatetimeIndex
1212
import pandas as pd
13-
import pandas.core.datetools as datetools
13+
import pandas.tseries.offsets as offsets
1414

1515
from pandas.util.testing import (assert_almost_equal,
1616
assert_series_equal,
@@ -136,14 +136,14 @@ def test_shift(self):
136136
assert_frame_equal(unshifted, self.tsframe)
137137

138138
# shift by DateOffset
139-
shiftedFrame = self.tsframe.shift(5, freq=datetools.BDay())
139+
shiftedFrame = self.tsframe.shift(5, freq=offsets.BDay())
140140
self.assertEqual(len(shiftedFrame), len(self.tsframe))
141141

142142
shiftedFrame2 = self.tsframe.shift(5, freq='B')
143143
assert_frame_equal(shiftedFrame, shiftedFrame2)
144144

145145
d = self.tsframe.index[0]
146-
shifted_d = d + datetools.BDay(5)
146+
shifted_d = d + offsets.BDay(5)
147147
assert_series_equal(self.tsframe.xs(d),
148148
shiftedFrame.xs(shifted_d), check_names=False)
149149

@@ -160,7 +160,7 @@ def test_shift(self):
160160
ps.ix[:-1, 0].values)
161161

162162
shifted2 = ps.shift(1, 'B')
163-
shifted3 = ps.shift(1, datetools.bday)
163+
shifted3 = ps.shift(1, offsets.BDay())
164164
assert_frame_equal(shifted2, shifted3)
165165
assert_frame_equal(ps, shifted2.shift(-1, 'B'))
166166

@@ -222,7 +222,7 @@ def test_tshift(self):
222222
shifted2 = ps.tshift(freq='B')
223223
assert_frame_equal(shifted, shifted2)
224224

225-
shifted3 = ps.tshift(freq=datetools.bday)
225+
shifted3 = ps.tshift(freq=offsets.BDay())
226226
assert_frame_equal(shifted, shifted3)
227227

228228
assertRaisesRegexp(ValueError, 'does not match', ps.tshift, freq='M')
@@ -297,7 +297,7 @@ def test_truncate_copy(self):
297297
self.assertFalse((self.tsframe.values[5:11] == 5).any())
298298

299299
def test_asfreq(self):
300-
offset_monthly = self.tsframe.asfreq(datetools.bmonthEnd)
300+
offset_monthly = self.tsframe.asfreq(offsets.BMonthEnd())
301301
rule_monthly = self.tsframe.asfreq('BM')
302302

303303
assert_almost_equal(offset_monthly['A'], rule_monthly['A'])
@@ -365,3 +365,9 @@ def test_operation_on_NaT(self):
365365
res = df.max()
366366
exp = pd.Series([pd.NaT], index=["foo"])
367367
tm.assert_series_equal(res, exp)
368+
369+
370+
if __name__ == '__main__':
371+
import nose
372+
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
373+
exit=False)

0 commit comments

Comments
 (0)