From 4001abec94b55d352ecacb947563d062d789371f Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 11 Feb 2019 09:26:25 -0800 Subject: [PATCH 1/2] avoid runtime/circular imports --- pandas/core/dtypes/dtypes.py | 10 ++++------ pandas/core/dtypes/missing.py | 8 ++++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index f187d786d9f61..640d43f3b0e03 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -8,7 +8,8 @@ from pandas._libs.interval import Interval from pandas._libs.tslibs import NaT, Period, Timestamp, timezones -from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCIndexClass +from pandas.core.dtypes.generic import ( + ABCCategoricalIndex, ABCDateOffset, ABCIndexClass) from pandas import compat @@ -758,8 +759,7 @@ def __new__(cls, freq=None): # empty constructor for pickle compat return object.__new__(cls) - from pandas.tseries.offsets import DateOffset - if not isinstance(freq, DateOffset): + if not isinstance(freq, ABCDateOffset): freq = cls._parse_dtype_strict(freq) try: @@ -790,12 +790,10 @@ def construct_from_string(cls, string): Strict construction from a string, raise a TypeError if not possible """ - from pandas.tseries.offsets import DateOffset - if (isinstance(string, compat.string_types) and (string.startswith('period[') or string.startswith('Period[')) or - isinstance(string, DateOffset)): + isinstance(string, ABCDateOffset)): # do not parse string like U as period[U] # avoid tuple to be regarded as freq try: diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 3c6d3f212342b..697c58a365233 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -221,8 +221,8 @@ def _isna_ndarraylike(obj): # box if isinstance(obj, ABCSeries): - from pandas import Series - result = Series(result, index=obj.index, name=obj.name, copy=False) + result = obj._constructor( + result, index=obj.index, name=obj.name, copy=False) return result @@ -250,8 +250,8 @@ def _isna_ndarraylike_old(obj): # box if isinstance(obj, ABCSeries): - from pandas import Series - result = Series(result, index=obj.index, name=obj.name, copy=False) + result = obj._constructor( + result, index=obj.index, name=obj.name, copy=False) return result From f95b5b3ef1b730e5db87216a889de2a4e7faf1a7 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 11 Feb 2019 09:48:21 -0800 Subject: [PATCH 2/2] Avoid runtime/circular imports --- pandas/core/tools/datetimes.py | 8 +++----- pandas/core/tools/timedeltas.py | 7 +++---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 3da349c570274..0c76ac6cd75ac 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -588,9 +588,8 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, if not cache_array.empty: result = arg.map(cache_array) else: - from pandas import Series values = convert_listlike(arg._values, True, format) - result = Series(values, index=arg.index, name=arg.name) + result = arg._constructor(values, index=arg.index, name=arg.name) elif isinstance(arg, (ABCDataFrame, compat.MutableMapping)): result = _assemble_from_unit_mappings(arg, errors, box, tz) elif isinstance(arg, ABCIndexClass): @@ -827,7 +826,6 @@ def to_time(arg, format=None, infer_time_format=False, errors='raise'): ------- datetime.time """ - from pandas.core.series import Series def _convert_listlike(arg, format): @@ -892,9 +890,9 @@ def _convert_listlike(arg, format): return arg elif isinstance(arg, time): return arg - elif isinstance(arg, Series): + elif isinstance(arg, ABCSeries): values = _convert_listlike(arg._values, format) - return Series(values, index=arg.index, name=arg.name) + return arg._constructor(values, index=arg.index, name=arg.name) elif isinstance(arg, ABCIndexClass): return _convert_listlike(arg, format) elif is_list_like(arg): diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index 30cb15f311b9f..7ebaf3056e79e 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -6,12 +6,12 @@ import numpy as np +from pandas._libs.tslibs import NaT from pandas._libs.tslibs.timedeltas import Timedelta, parse_timedelta_unit from pandas.core.dtypes.common import is_list_like from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries -import pandas as pd from pandas.core.arrays.timedeltas import sequence_to_td64ns @@ -100,10 +100,9 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'): if arg is None: return arg elif isinstance(arg, ABCSeries): - from pandas import Series values = _convert_listlike(arg._values, unit=unit, box=False, errors=errors) - return Series(values, index=arg.index, name=arg.name) + return arg._constructor(values, index=arg.index, name=arg.name) elif isinstance(arg, ABCIndexClass): return _convert_listlike(arg, unit=unit, box=box, errors=errors, name=arg.name) @@ -136,7 +135,7 @@ def _coerce_scalar_to_timedelta_type(r, unit='ns', box=True, errors='raise'): return r # coerce - result = pd.NaT + result = NaT return result