Skip to content

CLN: reorg pandas.tseries #16040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 18, 2017
2 changes: 1 addition & 1 deletion doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ The following methods are available only for ``DataFrameGroupBy`` objects.

Resampling
----------
.. currentmodule:: pandas.tseries.resample
.. currentmodule:: pandas.core.resample

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

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ from tslib cimport (
)

from pandas.tseries import offsets
from pandas.tseries.tools import parse_time_string
from pandas.core.tools.datetimes import parse_time_string
from pandas.tseries import frequencies

cdef int64_t NPY_NAT = util.get_nat()
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ class Timestamp(_Timestamp):
"""
Return an period of which this timestamp is an observation.
"""
from pandas.tseries.period import Period
from pandas import Period

if freq is None:
freq = self.freq
Expand Down
8 changes: 7 additions & 1 deletion pandas/compat/pickle_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ def load_reduce(self):
('pandas.indexes.range', 'RangeIndex'):
('pandas.core.indexes.range', 'RangeIndex'),
('pandas.indexes.multi', 'MultiIndex'):
('pandas.core.indexes.multi', 'MultiIndex')
('pandas.core.indexes.multi', 'MultiIndex'),
('pandas.tseries.index', '_new_DatetimeIndex'):
('pandas.core.indexes.datetimes', '_new_DatetimeIndex'),
('pandas.tseries.index', 'DatetimeIndex'):
('pandas.core.indexes.datetimes', 'DatetimeIndex'),
('pandas.tseries.period', 'PeriodIndex'):
('pandas.core.indexes.period', 'PeriodIndex')
}


Expand Down
17 changes: 10 additions & 7 deletions pandas/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
from pandas.io.formats.format import set_eng_float_format
from pandas.core.index import (Index, CategoricalIndex, Int64Index,
UInt64Index, RangeIndex, Float64Index,
MultiIndex, IntervalIndex)
MultiIndex, IntervalIndex,
TimedeltaIndex, DatetimeIndex,
PeriodIndex, NaT)
from pandas.core.indexes.period import Period, period_range, pnow
from pandas.core.indexes.timedeltas import Timedelta, timedelta_range
from pandas.core.indexes.datetimes import Timestamp, date_range, bdate_range
from pandas.core.indexes.interval import Interval, interval_range

from pandas.core.series import Series
Expand All @@ -23,13 +28,11 @@
lreshape, wide_to_long)

from pandas.core.indexing import IndexSlice
from pandas.core.dtypes.cast import to_numeric
from pandas.core.tools.numeric import to_numeric
from pandas.tseries.offsets import DateOffset
from pandas.tseries.tools import to_datetime
from pandas.tseries.index import (DatetimeIndex, Timestamp,
date_range, bdate_range)
from pandas.tseries.tdi import TimedeltaIndex, Timedelta
from pandas.tseries.period import Period, PeriodIndex
from pandas.core.tools.datetimes import to_datetime
from pandas.core.tools.timedeltas import to_timedelta
from pandas.core.resample import TimeGrouper

# see gh-14094.
from pandas.util.depr_module import _DeprecatedModule
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pandas.core.computation.ops import is_term, UndefinedVariableError
from pandas.core.computation.expr import BaseExprVisitor
from pandas.core.computation.common import _ensure_decoded
from pandas.tseries.timedeltas import _coerce_scalar_to_timedelta_type
from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type


class Scope(expr.Scope):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/datetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import warnings

from pandas.tseries.tools import *
from pandas.core.tools.datetimes import *
from pandas.tseries.offsets import *
from pandas.tseries.frequencies import *

Expand Down
171 changes: 5 additions & 166 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np
import warnings

import pandas as pd
from pandas._libs import tslib, lib
from pandas._libs.tslib import iNaT
from pandas.compat import string_types, text_type, PY3
Expand All @@ -19,8 +18,6 @@
is_integer_dtype,
is_datetime_or_timedelta_dtype,
is_bool_dtype, is_scalar,
is_numeric_dtype, is_decimal,
is_number,
_string_dtypes,
_coerce_to_dtype,
_ensure_int8, _ensure_int16,
Expand All @@ -29,7 +26,7 @@
_POSSIBLY_CAST_DTYPES)
from .dtypes import ExtensionDtype, DatetimeTZDtype, PeriodDtype
from .generic import (ABCDatetimeIndex, ABCPeriodIndex,
ABCSeries, ABCIndexClass)
ABCSeries)
from .missing import isnull, notnull
from .inference import is_list_like

Expand Down Expand Up @@ -548,7 +545,7 @@ def coerce_to_dtypes(result, dtypes):
if len(result) != len(dtypes):
raise AssertionError("_coerce_to_dtypes requires equal len arrays")

from pandas.tseries.timedeltas import _coerce_scalar_to_timedelta_type
from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type

def conv(r, dtype):
try:
Expand Down Expand Up @@ -670,7 +667,7 @@ def maybe_convert_objects(values, convert_dates=True, convert_numeric=True,
if convert_timedeltas and values.dtype == np.object_:

if convert_timedeltas == 'coerce':
from pandas.tseries.timedeltas import to_timedelta
from pandas.core.tools.timedeltas import to_timedelta
new_values = to_timedelta(values, coerce=True)

# if we are all nans then leave me alone
Expand Down Expand Up @@ -872,8 +869,8 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'):
""" try to cast the array/value to a datetimelike dtype, converting float
nan to iNaT
"""
from pandas.tseries.timedeltas import to_timedelta
from pandas.tseries.tools import to_datetime
from pandas.core.tools.timedeltas import to_timedelta
from pandas.core.tools.datetimes import to_datetime

if dtype is not None:
if isinstance(dtype, string_types):
Expand Down Expand Up @@ -1029,161 +1026,3 @@ def find_common_type(types):
return np.object

return np.find_common_type(types, [])


def to_numeric(arg, errors='raise', downcast=None):
"""
Convert argument to a numeric type.

Parameters
----------
arg : list, tuple, 1-d array, or Series
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
- If 'raise', then invalid parsing will raise an exception
- If 'coerce', then invalid parsing will be set as NaN
- If 'ignore', then invalid parsing will return the input
downcast : {'integer', 'signed', 'unsigned', 'float'} , default None
If not None, and if the data has been successfully cast to a
numerical dtype (or if the data was numeric to begin with),
downcast that resulting data to the smallest numerical dtype
possible according to the following rules:

- 'integer' or 'signed': smallest signed int dtype (min.: np.int8)
- 'unsigned': smallest unsigned int dtype (min.: np.uint8)
- 'float': smallest float dtype (min.: np.float32)

As this behaviour is separate from the core conversion to
numeric values, any errors raised during the downcasting
will be surfaced regardless of the value of the 'errors' input.

In addition, downcasting will only occur if the size
of the resulting data's dtype is strictly larger than
the dtype it is to be cast to, so if none of the dtypes
checked satisfy that specification, no downcasting will be
performed on the data.

.. versionadded:: 0.19.0

Returns
-------
ret : numeric if parsing succeeded.
Return type depends on input. Series if Series, otherwise ndarray

Examples
--------
Take separate series and convert to numeric, coercing when told to

>>> import pandas as pd
>>> s = pd.Series(['1.0', '2', -3])
>>> pd.to_numeric(s)
0 1.0
1 2.0
2 -3.0
dtype: float64
>>> pd.to_numeric(s, downcast='float')
0 1.0
1 2.0
2 -3.0
dtype: float32
>>> pd.to_numeric(s, downcast='signed')
0 1
1 2
2 -3
dtype: int8
>>> s = pd.Series(['apple', '1.0', '2', -3])
>>> pd.to_numeric(s, errors='ignore')
0 apple
1 1.0
2 2
3 -3
dtype: object
>>> pd.to_numeric(s, errors='coerce')
0 NaN
1 1.0
2 2.0
3 -3.0
dtype: float64
"""
if downcast not in (None, 'integer', 'signed', 'unsigned', 'float'):
raise ValueError('invalid downcasting method provided')

is_series = False
is_index = False
is_scalars = False

if isinstance(arg, ABCSeries):
is_series = True
values = arg.values
elif isinstance(arg, ABCIndexClass):
is_index = True
values = arg.asi8
if values is None:
values = arg.values
elif isinstance(arg, (list, tuple)):
values = np.array(arg, dtype='O')
elif is_scalar(arg):
if is_decimal(arg):
return float(arg)
if is_number(arg):
return arg
is_scalars = True
values = np.array([arg], dtype='O')
elif getattr(arg, 'ndim', 1) > 1:
raise TypeError('arg must be a list, tuple, 1-d array, or Series')
else:
values = arg

try:
if is_numeric_dtype(values):
pass
elif is_datetime_or_timedelta_dtype(values):
values = values.astype(np.int64)
else:
values = _ensure_object(values)
coerce_numeric = False if errors in ('ignore', 'raise') else True
values = lib.maybe_convert_numeric(values, set(),
coerce_numeric=coerce_numeric)

except Exception:
if errors == 'raise':
raise

# attempt downcast only if the data has been successfully converted
# to a numerical dtype and if a downcast method has been specified
if downcast is not None and is_numeric_dtype(values):
typecodes = None

if downcast in ('integer', 'signed'):
typecodes = np.typecodes['Integer']
elif downcast == 'unsigned' and np.min(values) >= 0:
typecodes = np.typecodes['UnsignedInteger']
elif downcast == 'float':
typecodes = np.typecodes['Float']

# pandas support goes only to np.float32,
# as float dtypes smaller than that are
# extremely rare and not well supported
float_32_char = np.dtype(np.float32).char
float_32_ind = typecodes.index(float_32_char)
typecodes = typecodes[float_32_ind:]

if typecodes is not None:
# from smallest to largest
for dtype in typecodes:
if np.dtype(dtype).itemsize <= values.dtype.itemsize:
values = maybe_downcast_to_dtype(values, dtype)

# successful conversion
if values.dtype == dtype:
break

if is_series:
return pd.Series(values, index=arg.index, name=arg.name)
elif is_index:
# because we want to coerce to numeric if possible,
# do not use _shallow_copy_with_infer
return pd.Index(values, name=arg.name)
elif is_scalars:
return values[0]
else:
return values
6 changes: 3 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@
from pandas.util.decorators import Appender, Substitution
from pandas.util.validators import validate_bool_kwarg

from pandas.tseries.period import PeriodIndex
from pandas.tseries.index import DatetimeIndex
from pandas.tseries.tdi import TimedeltaIndex
from pandas.core.indexes.period import PeriodIndex
from pandas.core.indexes.datetimes import DatetimeIndex
from pandas.core.indexes.timedeltas import TimedeltaIndex

import pandas.core.base as base
import pandas.core.common as com
Expand Down
12 changes: 6 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
from pandas.core.index import (Index, MultiIndex, _ensure_index,
InvalidIndexError)
import pandas.core.indexing as indexing
from pandas.tseries.index import DatetimeIndex
from pandas.tseries.period import PeriodIndex, Period
from pandas.core.indexes.datetimes import DatetimeIndex
from pandas.core.indexes.period import PeriodIndex, Period
from pandas.core.internals import BlockManager
import pandas.core.algorithms as algos
import pandas.core.common as com
Expand Down Expand Up @@ -4363,7 +4363,7 @@ def asfreq(self, freq, method=None, how=None, normalize=False,
To learn more about the frequency strings, please see `this link
<http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__.
"""
from pandas.tseries.resample import asfreq
from pandas.core.resample import asfreq
return asfreq(self, freq, method=method, how=how, normalize=normalize,
fill_value=fill_value)

Expand Down Expand Up @@ -4573,8 +4573,8 @@ def resample(self, rule, how=None, axis=0, fill_method=None, closed=None,
2000-01-01 00:00:00 0 6 12 18
2000-01-01 00:03:00 0 4 8 12
"""
from pandas.tseries.resample import (resample,
_maybe_process_deprecations)
from pandas.core.resample import (resample,
_maybe_process_deprecations)
axis = self._get_axis_number(axis)
r = resample(self, freq=rule, label=label, closed=closed,
axis=axis, kind=kind, loffset=loffset,
Expand Down Expand Up @@ -5361,7 +5361,7 @@ def truncate(self, before=None, after=None, axis=None, copy=True):
# if we have a date index, convert to dates, otherwise
# treat like a slice
if ax.is_all_dates:
from pandas.tseries.tools import to_datetime
from pandas.core.tools.datetimes import to_datetime
before = to_datetime(before)
after = to_datetime(after)

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class Grouper(object):

def __new__(cls, *args, **kwargs):
if kwargs.get('freq') is not None:
from pandas.tseries.resample import TimeGrouper
from pandas.core.resample import TimeGrouper
cls = TimeGrouper
return super(Grouper, cls).__new__(cls)

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

@Substitution(name='groupby')
Expand Down Expand Up @@ -3509,7 +3509,7 @@ def _decide_output_index(self, output, labels):

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

if len(keys) == 0:
return DataFrame(index=keys)
Expand Down
Loading