Skip to content

Commit 1d2a0c9

Browse files
jrebackanalyticalmonk
authored and
analyticalmonk
committed
CLN: reorg pandas.tseries (pandas-dev#16040)
* CLN: move pandas/tseries/resample.py -> pandas/core/resample.py closes pandas-dev#13634 * CLN: move pandas.tseries.period -> pandas.core.indexes.period * CLN: move pandas.tseries.tdi -> pandas.core.indexes.timedeltas * CLN: move pandas.tseries.base -> pandas.core.indexes.datetimelike * CLN: pandas.tseries.common -> pandas.core.indexes.accessors * CLN: move pandas.tseries.index -> pandas.core.indexes.datetimes * CLN: move pandas.tseries.timedeltas, pandas.tseries.tools -> pandas.core.tools * move to_numeric to pandas.core.tools.numeric
1 parent cfcc4c2 commit 1d2a0c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+727
-697
lines changed

doc/source/api.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ The following methods are available only for ``DataFrameGroupBy`` objects.
17611761

17621762
Resampling
17631763
----------
1764-
.. currentmodule:: pandas.tseries.resample
1764+
.. currentmodule:: pandas.core.resample
17651765

17661766
Resampler objects are returned by resample calls: :func:`pandas.DataFrame.resample`, :func:`pandas.Series.resample`.
17671767

pandas/_libs/period.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ from tslib cimport (
3434
)
3535

3636
from pandas.tseries import offsets
37-
from pandas.tseries.tools import parse_time_string
37+
from pandas.core.tools.datetimes import parse_time_string
3838
from pandas.tseries import frequencies
3939

4040
cdef int64_t NPY_NAT = util.get_nat()

pandas/_libs/tslib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ class Timestamp(_Timestamp):
502502
"""
503503
Return an period of which this timestamp is an observation.
504504
"""
505-
from pandas.tseries.period import Period
505+
from pandas import Period
506506

507507
if freq is None:
508508
freq = self.freq

pandas/compat/pickle_compat.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ def load_reduce(self):
9494
('pandas.indexes.range', 'RangeIndex'):
9595
('pandas.core.indexes.range', 'RangeIndex'),
9696
('pandas.indexes.multi', 'MultiIndex'):
97-
('pandas.core.indexes.multi', 'MultiIndex')
97+
('pandas.core.indexes.multi', 'MultiIndex'),
98+
('pandas.tseries.index', '_new_DatetimeIndex'):
99+
('pandas.core.indexes.datetimes', '_new_DatetimeIndex'),
100+
('pandas.tseries.index', 'DatetimeIndex'):
101+
('pandas.core.indexes.datetimes', 'DatetimeIndex'),
102+
('pandas.tseries.period', 'PeriodIndex'):
103+
('pandas.core.indexes.period', 'PeriodIndex')
98104
}
99105

100106

pandas/core/api.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
from pandas.io.formats.format import set_eng_float_format
1212
from pandas.core.index import (Index, CategoricalIndex, Int64Index,
1313
UInt64Index, RangeIndex, Float64Index,
14-
MultiIndex, IntervalIndex)
14+
MultiIndex, IntervalIndex,
15+
TimedeltaIndex, DatetimeIndex,
16+
PeriodIndex, NaT)
17+
from pandas.core.indexes.period import Period, period_range, pnow
18+
from pandas.core.indexes.timedeltas import Timedelta, timedelta_range
19+
from pandas.core.indexes.datetimes import Timestamp, date_range, bdate_range
1520
from pandas.core.indexes.interval import Interval, interval_range
1621

1722
from pandas.core.series import Series
@@ -23,13 +28,11 @@
2328
lreshape, wide_to_long)
2429

2530
from pandas.core.indexing import IndexSlice
26-
from pandas.core.dtypes.cast import to_numeric
31+
from pandas.core.tools.numeric import to_numeric
2732
from pandas.tseries.offsets import DateOffset
28-
from pandas.tseries.tools import to_datetime
29-
from pandas.tseries.index import (DatetimeIndex, Timestamp,
30-
date_range, bdate_range)
31-
from pandas.tseries.tdi import TimedeltaIndex, Timedelta
32-
from pandas.tseries.period import Period, PeriodIndex
33+
from pandas.core.tools.datetimes import to_datetime
34+
from pandas.core.tools.timedeltas import to_timedelta
35+
from pandas.core.resample import TimeGrouper
3336

3437
# see gh-14094.
3538
from pandas.util.depr_module import _DeprecatedModule

pandas/core/computation/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pandas.core.computation.ops import is_term, UndefinedVariableError
1515
from pandas.core.computation.expr import BaseExprVisitor
1616
from pandas.core.computation.common import _ensure_decoded
17-
from pandas.tseries.timedeltas import _coerce_scalar_to_timedelta_type
17+
from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type
1818

1919

2020
class Scope(expr.Scope):

pandas/core/datetools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import warnings
66

7-
from pandas.tseries.tools import *
7+
from pandas.core.tools.datetimes import *
88
from pandas.tseries.offsets import *
99
from pandas.tseries.frequencies import *
1010

pandas/core/dtypes/cast.py

+5-166
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import numpy as np
66
import warnings
77

8-
import pandas as pd
98
from pandas._libs import tslib, lib
109
from pandas._libs.tslib import iNaT
1110
from pandas.compat import string_types, text_type, PY3
@@ -19,8 +18,6 @@
1918
is_integer_dtype,
2019
is_datetime_or_timedelta_dtype,
2120
is_bool_dtype, is_scalar,
22-
is_numeric_dtype, is_decimal,
23-
is_number,
2421
_string_dtypes,
2522
_coerce_to_dtype,
2623
pandas_dtype,
@@ -30,7 +27,7 @@
3027
_POSSIBLY_CAST_DTYPES)
3128
from .dtypes import ExtensionDtype, DatetimeTZDtype, PeriodDtype
3229
from .generic import (ABCDatetimeIndex, ABCPeriodIndex,
33-
ABCSeries, ABCIndexClass)
30+
ABCSeries)
3431
from .missing import isnull, notnull
3532
from .inference import is_list_like
3633

@@ -549,7 +546,7 @@ def coerce_to_dtypes(result, dtypes):
549546
if len(result) != len(dtypes):
550547
raise AssertionError("_coerce_to_dtypes requires equal len arrays")
551548

552-
from pandas.tseries.timedeltas import _coerce_scalar_to_timedelta_type
549+
from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type
553550

554551
def conv(r, dtype):
555552
try:
@@ -671,7 +668,7 @@ def maybe_convert_objects(values, convert_dates=True, convert_numeric=True,
671668
if convert_timedeltas and values.dtype == np.object_:
672669

673670
if convert_timedeltas == 'coerce':
674-
from pandas.tseries.timedeltas import to_timedelta
671+
from pandas.core.tools.timedeltas import to_timedelta
675672
new_values = to_timedelta(values, coerce=True)
676673

677674
# if we are all nans then leave me alone
@@ -873,8 +870,8 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'):
873870
""" try to cast the array/value to a datetimelike dtype, converting float
874871
nan to iNaT
875872
"""
876-
from pandas.tseries.timedeltas import to_timedelta
877-
from pandas.tseries.tools import to_datetime
873+
from pandas.core.tools.timedeltas import to_timedelta
874+
from pandas.core.tools.datetimes import to_datetime
878875

879876
if dtype is not None:
880877
if isinstance(dtype, string_types):
@@ -1030,161 +1027,3 @@ def find_common_type(types):
10301027
return np.object
10311028

10321029
return np.find_common_type(types, [])
1033-
1034-
1035-
def to_numeric(arg, errors='raise', downcast=None):
1036-
"""
1037-
Convert argument to a numeric type.
1038-
1039-
Parameters
1040-
----------
1041-
arg : list, tuple, 1-d array, or Series
1042-
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
1043-
- If 'raise', then invalid parsing will raise an exception
1044-
- If 'coerce', then invalid parsing will be set as NaN
1045-
- If 'ignore', then invalid parsing will return the input
1046-
downcast : {'integer', 'signed', 'unsigned', 'float'} , default None
1047-
If not None, and if the data has been successfully cast to a
1048-
numerical dtype (or if the data was numeric to begin with),
1049-
downcast that resulting data to the smallest numerical dtype
1050-
possible according to the following rules:
1051-
1052-
- 'integer' or 'signed': smallest signed int dtype (min.: np.int8)
1053-
- 'unsigned': smallest unsigned int dtype (min.: np.uint8)
1054-
- 'float': smallest float dtype (min.: np.float32)
1055-
1056-
As this behaviour is separate from the core conversion to
1057-
numeric values, any errors raised during the downcasting
1058-
will be surfaced regardless of the value of the 'errors' input.
1059-
1060-
In addition, downcasting will only occur if the size
1061-
of the resulting data's dtype is strictly larger than
1062-
the dtype it is to be cast to, so if none of the dtypes
1063-
checked satisfy that specification, no downcasting will be
1064-
performed on the data.
1065-
1066-
.. versionadded:: 0.19.0
1067-
1068-
Returns
1069-
-------
1070-
ret : numeric if parsing succeeded.
1071-
Return type depends on input. Series if Series, otherwise ndarray
1072-
1073-
Examples
1074-
--------
1075-
Take separate series and convert to numeric, coercing when told to
1076-
1077-
>>> import pandas as pd
1078-
>>> s = pd.Series(['1.0', '2', -3])
1079-
>>> pd.to_numeric(s)
1080-
0 1.0
1081-
1 2.0
1082-
2 -3.0
1083-
dtype: float64
1084-
>>> pd.to_numeric(s, downcast='float')
1085-
0 1.0
1086-
1 2.0
1087-
2 -3.0
1088-
dtype: float32
1089-
>>> pd.to_numeric(s, downcast='signed')
1090-
0 1
1091-
1 2
1092-
2 -3
1093-
dtype: int8
1094-
>>> s = pd.Series(['apple', '1.0', '2', -3])
1095-
>>> pd.to_numeric(s, errors='ignore')
1096-
0 apple
1097-
1 1.0
1098-
2 2
1099-
3 -3
1100-
dtype: object
1101-
>>> pd.to_numeric(s, errors='coerce')
1102-
0 NaN
1103-
1 1.0
1104-
2 2.0
1105-
3 -3.0
1106-
dtype: float64
1107-
"""
1108-
if downcast not in (None, 'integer', 'signed', 'unsigned', 'float'):
1109-
raise ValueError('invalid downcasting method provided')
1110-
1111-
is_series = False
1112-
is_index = False
1113-
is_scalars = False
1114-
1115-
if isinstance(arg, ABCSeries):
1116-
is_series = True
1117-
values = arg.values
1118-
elif isinstance(arg, ABCIndexClass):
1119-
is_index = True
1120-
values = arg.asi8
1121-
if values is None:
1122-
values = arg.values
1123-
elif isinstance(arg, (list, tuple)):
1124-
values = np.array(arg, dtype='O')
1125-
elif is_scalar(arg):
1126-
if is_decimal(arg):
1127-
return float(arg)
1128-
if is_number(arg):
1129-
return arg
1130-
is_scalars = True
1131-
values = np.array([arg], dtype='O')
1132-
elif getattr(arg, 'ndim', 1) > 1:
1133-
raise TypeError('arg must be a list, tuple, 1-d array, or Series')
1134-
else:
1135-
values = arg
1136-
1137-
try:
1138-
if is_numeric_dtype(values):
1139-
pass
1140-
elif is_datetime_or_timedelta_dtype(values):
1141-
values = values.astype(np.int64)
1142-
else:
1143-
values = _ensure_object(values)
1144-
coerce_numeric = False if errors in ('ignore', 'raise') else True
1145-
values = lib.maybe_convert_numeric(values, set(),
1146-
coerce_numeric=coerce_numeric)
1147-
1148-
except Exception:
1149-
if errors == 'raise':
1150-
raise
1151-
1152-
# attempt downcast only if the data has been successfully converted
1153-
# to a numerical dtype and if a downcast method has been specified
1154-
if downcast is not None and is_numeric_dtype(values):
1155-
typecodes = None
1156-
1157-
if downcast in ('integer', 'signed'):
1158-
typecodes = np.typecodes['Integer']
1159-
elif downcast == 'unsigned' and np.min(values) >= 0:
1160-
typecodes = np.typecodes['UnsignedInteger']
1161-
elif downcast == 'float':
1162-
typecodes = np.typecodes['Float']
1163-
1164-
# pandas support goes only to np.float32,
1165-
# as float dtypes smaller than that are
1166-
# extremely rare and not well supported
1167-
float_32_char = np.dtype(np.float32).char
1168-
float_32_ind = typecodes.index(float_32_char)
1169-
typecodes = typecodes[float_32_ind:]
1170-
1171-
if typecodes is not None:
1172-
# from smallest to largest
1173-
for dtype in typecodes:
1174-
if np.dtype(dtype).itemsize <= values.dtype.itemsize:
1175-
values = maybe_downcast_to_dtype(values, dtype)
1176-
1177-
# successful conversion
1178-
if values.dtype == dtype:
1179-
break
1180-
1181-
if is_series:
1182-
return pd.Series(values, index=arg.index, name=arg.name)
1183-
elif is_index:
1184-
# because we want to coerce to numeric if possible,
1185-
# do not use _shallow_copy_with_infer
1186-
return pd.Index(values, name=arg.name)
1187-
elif is_scalars:
1188-
return values[0]
1189-
else:
1190-
return values

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@
8282
from pandas.util.decorators import Appender, Substitution
8383
from pandas.util.validators import validate_bool_kwarg
8484

85-
from pandas.tseries.period import PeriodIndex
86-
from pandas.tseries.index import DatetimeIndex
87-
from pandas.tseries.tdi import TimedeltaIndex
85+
from pandas.core.indexes.period import PeriodIndex
86+
from pandas.core.indexes.datetimes import DatetimeIndex
87+
from pandas.core.indexes.timedeltas import TimedeltaIndex
8888

8989
import pandas.core.base as base
9090
import pandas.core.common as com

pandas/core/generic.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
from pandas.core.index import (Index, MultiIndex, _ensure_index,
3939
InvalidIndexError)
4040
import pandas.core.indexing as indexing
41-
from pandas.tseries.index import DatetimeIndex
42-
from pandas.tseries.period import PeriodIndex, Period
41+
from pandas.core.indexes.datetimes import DatetimeIndex
42+
from pandas.core.indexes.period import PeriodIndex, Period
4343
from pandas.core.internals import BlockManager
4444
import pandas.core.algorithms as algos
4545
import pandas.core.common as com
@@ -4366,7 +4366,7 @@ def asfreq(self, freq, method=None, how=None, normalize=False,
43664366
To learn more about the frequency strings, please see `this link
43674367
<http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__.
43684368
"""
4369-
from pandas.tseries.resample import asfreq
4369+
from pandas.core.resample import asfreq
43704370
return asfreq(self, freq, method=method, how=how, normalize=normalize,
43714371
fill_value=fill_value)
43724372

@@ -4576,8 +4576,8 @@ def resample(self, rule, how=None, axis=0, fill_method=None, closed=None,
45764576
2000-01-01 00:00:00 0 6 12 18
45774577
2000-01-01 00:03:00 0 4 8 12
45784578
"""
4579-
from pandas.tseries.resample import (resample,
4580-
_maybe_process_deprecations)
4579+
from pandas.core.resample import (resample,
4580+
_maybe_process_deprecations)
45814581
axis = self._get_axis_number(axis)
45824582
r = resample(self, freq=rule, label=label, closed=closed,
45834583
axis=axis, kind=kind, loffset=loffset,
@@ -5364,7 +5364,7 @@ def truncate(self, before=None, after=None, axis=None, copy=True):
53645364
# if we have a date index, convert to dates, otherwise
53655365
# treat like a slice
53665366
if ax.is_all_dates:
5367-
from pandas.tseries.tools import to_datetime
5367+
from pandas.core.tools.datetimes import to_datetime
53685368
before = to_datetime(before)
53695369
after = to_datetime(after)
53705370

pandas/core/groupby.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class Grouper(object):
232232

233233
def __new__(cls, *args, **kwargs):
234234
if kwargs.get('freq') is not None:
235-
from pandas.tseries.resample import TimeGrouper
235+
from pandas.core.resample import TimeGrouper
236236
cls = TimeGrouper
237237
return super(Grouper, cls).__new__(cls)
238238

@@ -1227,7 +1227,7 @@ def resample(self, rule, *args, **kwargs):
12271227
Provide resampling when using a TimeGrouper
12281228
Return a new grouper with our resampler appended
12291229
"""
1230-
from pandas.tseries.resample import get_resampler_for_grouping
1230+
from pandas.core.resample import get_resampler_for_grouping
12311231
return get_resampler_for_grouping(self, rule, *args, **kwargs)
12321232

12331233
@Substitution(name='groupby')
@@ -3509,7 +3509,7 @@ def _decide_output_index(self, output, labels):
35093509

35103510
def _wrap_applied_output(self, keys, values, not_indexed_same=False):
35113511
from pandas.core.index import _all_indexes_same
3512-
from pandas.core.dtypes.cast import to_numeric
3512+
from pandas.core.tools.numeric import to_numeric
35133513

35143514
if len(keys) == 0:
35153515
return DataFrame(index=keys)

0 commit comments

Comments
 (0)