diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index a7379376c2f78..2febd36c76b75 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -7,15 +7,17 @@ import numpy as np -from pandas._libs import lib, tslib, tslibs +from pandas._libs import lib, tslib from pandas._libs.tslibs import ( NaT, OutOfBoundsDatetime, Period, Timedelta, Timestamp, + conversion, iNaT, ints_to_pydatetime, + ints_to_pytimedelta, ) from pandas._libs.tslibs.timezones import tz_compare from pandas._typing import ArrayLike, Dtype, DtypeObj @@ -552,7 +554,7 @@ def maybe_promote(dtype, fill_value=np.nan): dtype = np.dtype(np.object_) else: try: - fill_value = tslibs.Timestamp(fill_value).to_datetime64() + fill_value = Timestamp(fill_value).to_datetime64() except (TypeError, ValueError): dtype = np.dtype(np.object_) elif issubclass(dtype.type, np.timedelta64): @@ -565,7 +567,7 @@ def maybe_promote(dtype, fill_value=np.nan): dtype = np.dtype(np.object_) else: try: - fv = tslibs.Timedelta(fill_value) + fv = Timedelta(fill_value) except ValueError: dtype = np.dtype(np.object_) else: @@ -738,8 +740,8 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, dtype = np.dtype(object) elif isinstance(val, (np.datetime64, datetime)): - val = tslibs.Timestamp(val) - if val is tslibs.NaT or val.tz is None: + val = Timestamp(val) + if val is NaT or val.tz is None: dtype = np.dtype("M8[ns]") else: if pandas_dtype: @@ -750,7 +752,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, val = val.value elif isinstance(val, (np.timedelta64, timedelta)): - val = tslibs.Timedelta(val).value + val = Timedelta(val).value dtype = np.dtype("m8[ns]") elif is_bool(val): @@ -941,9 +943,9 @@ def conv(r, dtype): if np.any(isna(r)): pass elif dtype == DT64NS_DTYPE: - r = tslibs.Timestamp(r) + r = Timestamp(r) elif dtype == TD64NS_DTYPE: - r = tslibs.Timedelta(r) + r = Timedelta(r) elif dtype == np.bool_: # messy. non 0/1 integers do not get converted. if is_integer(r) and r not in [0, 1]: @@ -1006,7 +1008,7 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False): elif is_timedelta64_dtype(arr): if is_object_dtype(dtype): - return tslibs.ints_to_pytimedelta(arr.view(np.int64)) + return ints_to_pytimedelta(arr.view(np.int64)) elif dtype == np.int64: if isna(arr).any(): raise ValueError("Cannot convert NaT values to integer") @@ -1317,8 +1319,6 @@ def try_datetime(v): # we might have a sequence of the same-datetimes with tz's # if so coerce to a DatetimeIndex; if they are not the same, # then these stay as object dtype, xref GH19671 - from pandas._libs.tslibs import conversion - from pandas import DatetimeIndex try: @@ -1492,7 +1492,7 @@ def maybe_cast_to_datetime(value, dtype, errors: str = "raise"): dtype = value.dtype if dtype.kind == "M" and dtype != DT64NS_DTYPE: - value = tslibs.conversion.ensure_datetime64ns(value) + value = conversion.ensure_datetime64ns(value) elif dtype.kind == "m" and dtype != TD64NS_DTYPE: value = to_timedelta(value) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index bf8d50db8416e..28b3a2414679b 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -406,8 +406,6 @@ def __repr__(self) -> str_type: @staticmethod def _hash_categories(categories, ordered: Ordered = True) -> int: - from pandas.core.dtypes.common import DT64NS_DTYPE, is_datetime64tz_dtype - from pandas.core.util.hashing import ( combine_hash_arrays, hash_array, @@ -430,9 +428,9 @@ def _hash_categories(categories, ordered: Ordered = True) -> int: hashed = hash((tuple(categories), ordered)) return hashed - if is_datetime64tz_dtype(categories.dtype): + if DatetimeTZDtype.is_dtype(categories.dtype): # Avoid future warning. - categories = categories.astype(DT64NS_DTYPE) + categories = categories.astype("datetime64[ns]") cat_array = hash_array(np.asarray(categories), categorize=False) if ordered: @@ -1011,11 +1009,7 @@ class IntervalDtype(PandasExtensionDtype): _cache: Dict[str_type, PandasExtensionDtype] = {} def __new__(cls, subtype=None): - from pandas.core.dtypes.common import ( - is_categorical_dtype, - is_string_dtype, - pandas_dtype, - ) + from pandas.core.dtypes.common import is_string_dtype, pandas_dtype if isinstance(subtype, IntervalDtype): return subtype @@ -1038,7 +1032,7 @@ def __new__(cls, subtype=None): except TypeError as err: raise TypeError("could not construct IntervalDtype") from err - if is_categorical_dtype(subtype) or is_string_dtype(subtype): + if CategoricalDtype.is_dtype(subtype) or is_string_dtype(subtype): # GH 19016 msg = ( "category, object, and string subtypes are not supported " diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ba2f11c87369f..f6192a3ccdd7e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -147,6 +147,7 @@ ) from pandas.core.reshape.melt import melt from pandas.core.series import Series +from pandas.core.sorting import get_group_index, lexsort_indexer, nargsort from pandas.io.common import get_filepath_or_buffer from pandas.io.formats import console, format as fmt @@ -5251,8 +5252,6 @@ def duplicated( """ from pandas._libs.hashtable import SIZE_HINT_LIMIT, duplicated_int64 - from pandas.core.sorting import get_group_index - if self.empty: return self._constructor_sliced(dtype=bool) @@ -5315,7 +5314,6 @@ def sort_values( # type: ignore[override] f"Length of ascending ({len(ascending)}) != length of by ({len(by)})" ) if len(by) > 1: - from pandas.core.sorting import lexsort_indexer keys = [self._get_label_or_level_values(x, axis=axis) for x in by] @@ -5328,7 +5326,6 @@ def sort_values( # type: ignore[override] ) indexer = ensure_platform_int(indexer) else: - from pandas.core.sorting import nargsort by = by[0] k = self._get_label_or_level_values(by, axis=axis) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 7d0b0d6683e21..6b8f6b47ef882 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -34,7 +34,7 @@ from pandas._config import config from pandas._libs import lib -from pandas._libs.tslibs import Tick, Timestamp, to_offset +from pandas._libs.tslibs import Period, Tick, Timestamp, to_offset from pandas._typing import ( Axis, CompressionOptions, @@ -86,17 +86,21 @@ from pandas.core.dtypes.missing import isna, notna import pandas as pd -from pandas.core import missing, nanops +from pandas.core import indexing, missing, nanops import pandas.core.algorithms as algos from pandas.core.base import PandasObject, SelectionMixin import pandas.core.common as com from pandas.core.construction import create_series_with_explicit_dtype from pandas.core.flags import Flags from pandas.core.indexes import base as ibase -from pandas.core.indexes.api import Index, MultiIndex, RangeIndex, ensure_index -from pandas.core.indexes.datetimes import DatetimeIndex -from pandas.core.indexes.period import Period, PeriodIndex -import pandas.core.indexing as indexing +from pandas.core.indexes.api import ( + DatetimeIndex, + Index, + MultiIndex, + PeriodIndex, + RangeIndex, + ensure_index, +) from pandas.core.internals import BlockManager from pandas.core.missing import find_valid_index from pandas.core.ops import align_method_FRAME diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 7d3c2c2297d5d..f885e03f21470 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -23,15 +23,13 @@ from pandas._libs import algos as libalgos, index as libindex, lib import pandas._libs.join as libjoin from pandas._libs.lib import is_datetime_array, no_default -from pandas._libs.tslibs import OutOfBoundsDatetime, Timestamp -from pandas._libs.tslibs.period import IncompatibleFrequency +from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime, Timestamp from pandas._libs.tslibs.timezones import tz_compare from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label from pandas.compat.numpy import function as nv from pandas.errors import DuplicateLabelError, InvalidIndexError from pandas.util._decorators import Appender, cache_readonly, doc -from pandas.core.dtypes import concat as _concat from pandas.core.dtypes.cast import ( maybe_cast_to_integer_array, validate_numeric_casting, @@ -77,7 +75,7 @@ ) from pandas.core.dtypes.missing import array_equivalent, isna -from pandas.core import ops +from pandas.core import missing, ops from pandas.core.accessor import CachedAccessor import pandas.core.algorithms as algos from pandas.core.arrays import Categorical, ExtensionArray @@ -86,7 +84,6 @@ import pandas.core.common as com from pandas.core.indexers import deprecate_ndim_indexing from pandas.core.indexes.frozen import FrozenList -import pandas.core.missing as missing from pandas.core.ops import get_op_result_name from pandas.core.ops.invalid import make_invalid_op from pandas.core.sorting import ensure_key_mapped, nargsort @@ -4205,7 +4202,7 @@ def _concat(self, to_concat, name): """ to_concat = [x._values if isinstance(x, Index) else x for x in to_concat] - result = _concat.concat_compat(to_concat) + result = concat_compat(to_concat) return Index(result, name=name) def putmask(self, mask, value): diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 2ce4538a63d25..3153a9ce66b95 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -3093,7 +3093,6 @@ def get_locs(self, seq): >>> mi.get_locs([[True, False, True], slice('e', 'f')]) # doctest: +SKIP array([2], dtype=int64) """ - from pandas.core.indexes.numeric import Int64Index # must be lexsorted to at least as many levels true_slices = [i for (i, s) in enumerate(com.is_true_slices(seq)) if s] diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 61d9833a50a9f..ab349ce3539aa 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -6,8 +6,7 @@ import numpy as np -from pandas._libs import NaT, algos as libalgos, lib, writers -import pandas._libs.internals as libinternals +from pandas._libs import NaT, algos as libalgos, internals as libinternals, lib, writers from pandas._libs.internals import BlockPlacement from pandas._libs.tslibs import conversion from pandas._libs.tslibs.timezones import tz_compare diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 493ba87565220..5012be593820e 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -11,8 +11,7 @@ import numpy as np -from pandas._libs import Timedelta, hashtable as libhashtable, lib -import pandas._libs.join as libjoin +from pandas._libs import Timedelta, hashtable as libhashtable, join as libjoin, lib from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion from pandas.errors import MergeError from pandas.util._decorators import Appender, Substitution diff --git a/pandas/core/series.py b/pandas/core/series.py index fb684da20bac8..55aa32dd028ef 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -67,7 +67,6 @@ remove_na_arraylike, ) -import pandas as pd from pandas.core import algorithms, base, generic, nanops, ops from pandas.core.accessor import CachedAccessor from pandas.core.aggregation import aggregate, transform @@ -76,6 +75,7 @@ from pandas.core.arrays.sparse import SparseAccessor import pandas.core.common as com from pandas.core.construction import ( + array as pd_array, create_series_with_explicit_dtype, extract_array, is_empty_data, @@ -4193,7 +4193,7 @@ def f(x): if len(mapped) and isinstance(mapped[0], Series): # GH 25959 use pd.array instead of tolist # so extension arrays can be used - return self._constructor_expanddim(pd.array(mapped), index=self.index) + return self._constructor_expanddim(pd_array(mapped), index=self.index) else: return self._constructor(mapped, index=self.index).__finalize__( self, method="apply" diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 7b384c9bbb47d..1553deeef4059 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -16,8 +16,16 @@ import numpy as np -from pandas._libs import tslib, tslibs -from pandas._libs.tslibs import Timestamp, conversion, parsing +from pandas._libs import tslib +from pandas._libs.tslibs import ( + OutOfBoundsDatetime, + Timedelta, + Timestamp, + conversion, + iNaT, + nat_strings, + parsing, +) from pandas._libs.tslibs.parsing import ( # noqa DateParseError, format_is_iso, @@ -404,7 +412,7 @@ def _convert_listlike_datetimes( # datetime64[ns] orig_arg = ensure_object(orig_arg) result = _attempt_YYYYMMDD(orig_arg, errors=errors) - except (ValueError, TypeError, tslibs.OutOfBoundsDatetime) as err: + except (ValueError, TypeError, OutOfBoundsDatetime) as err: raise ValueError( "cannot convert the input to '%Y%m%d' date format" ) from err @@ -419,13 +427,13 @@ def _convert_listlike_datetimes( return _return_parsed_timezone_results( result, timezones, tz, name ) - except tslibs.OutOfBoundsDatetime: + except OutOfBoundsDatetime: if errors == "raise": raise elif errors == "coerce": result = np.empty(arg.shape, dtype="M8[ns]") iresult = result.view("i8") - iresult.fill(tslibs.iNaT) + iresult.fill(iNaT) else: result = arg except ValueError: @@ -438,7 +446,7 @@ def _convert_listlike_datetimes( elif errors == "coerce": result = np.empty(arg.shape, dtype="M8[ns]") iresult = result.view("i8") - iresult.fill(tslibs.iNaT) + iresult.fill(iNaT) else: result = arg except ValueError as e: @@ -508,7 +516,7 @@ def _adjust_to_origin(arg, origin, unit): j_max = Timestamp.max.to_julian_date() - j0 j_min = Timestamp.min.to_julian_date() - j0 if np.any(arg > j_max) or np.any(arg < j_min): - raise tslibs.OutOfBoundsDatetime( + raise OutOfBoundsDatetime( f"{original} is Out of Bounds for origin='julian'" ) else: @@ -525,10 +533,8 @@ def _adjust_to_origin(arg, origin, unit): # we are going to offset back to unix / epoch time try: offset = Timestamp(origin) - except tslibs.OutOfBoundsDatetime as err: - raise tslibs.OutOfBoundsDatetime( - f"origin {origin} is Out of Bounds" - ) from err + except OutOfBoundsDatetime as err: + raise OutOfBoundsDatetime(f"origin {origin} is Out of Bounds") from err except ValueError as err: raise ValueError( f"origin {origin} cannot be converted to a Timestamp" @@ -540,7 +546,7 @@ def _adjust_to_origin(arg, origin, unit): # convert the offset to the unit of the arg # this should be lossless in terms of precision - offset = offset // tslibs.Timedelta(1, unit=unit) + offset = offset // Timedelta(1, unit=unit) # scalars & ndarray-like can handle the addition if is_list_like(arg) and not isinstance(arg, (ABCSeries, Index, np.ndarray)): @@ -809,7 +815,7 @@ def to_datetime( elif is_list_like(arg): try: cache_array = _maybe_cache(arg, format, cache, convert_listlike) - except tslibs.OutOfBoundsDatetime: + except OutOfBoundsDatetime: # caching attempts to create a DatetimeIndex, which may raise # an OOB. If that's the desired behavior, then just reraise... if errors == "raise": @@ -965,7 +971,7 @@ def calc(carg): def calc_with_mask(carg, mask): result = np.empty(carg.shape, dtype="M8[ns]") iresult = result.view("i8") - iresult[~mask] = tslibs.iNaT + iresult[~mask] = iNaT masked_result = calc(carg[mask].astype(np.float64).astype(np.int64)) result[mask] = masked_result.astype("M8[ns]") @@ -986,7 +992,7 @@ def calc_with_mask(carg, mask): # string with NaN-like try: - mask = ~algorithms.isin(arg, list(tslibs.nat_strings)) + mask = ~algorithms.isin(arg, list(nat_strings)) return calc_with_mask(arg, mask) except (ValueError, OverflowError, TypeError): pass diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index cff4695603d06..32ca83787c4c1 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -186,7 +186,7 @@ def to_numeric(arg, errors="raise", downcast=None): break if is_series: - return pd.Series(values, index=arg.index, name=arg.name) + return arg._constructor(values, index=arg.index, name=arg.name) elif is_index: # because we want to coerce to numeric if possible, # do not use _shallow_copy diff --git a/pandas/core/tools/times.py b/pandas/core/tools/times.py index 3bac4cf0edb63..643c1165180b4 100644 --- a/pandas/core/tools/times.py +++ b/pandas/core/tools/times.py @@ -5,11 +5,9 @@ from pandas._libs.lib import is_list_like -from pandas.core.dtypes.generic import ABCSeries +from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries from pandas.core.dtypes.missing import notna -from pandas.core.indexes.base import Index - def to_time(arg, format=None, infer_time_format=False, errors="raise"): """ @@ -105,7 +103,7 @@ def _convert_listlike(arg, format): elif isinstance(arg, ABCSeries): values = _convert_listlike(arg._values, format) return arg._constructor(values, index=arg.index, name=arg.name) - elif isinstance(arg, Index): + elif isinstance(arg, ABCIndexClass): return _convert_listlike(arg, format) elif is_list_like(arg): return _convert_listlike(arg, format)