Skip to content

Commit

Permalink
remove uses of (ts)?lib.(NaT|iNaT|Timestamp) (#23562)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Nov 8, 2018
1 parent 8212001 commit 712fa94
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 149 deletions.
10 changes: 5 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np
import pandas as pd

from pandas._libs import tslib, properties
from pandas._libs import properties, Timestamp, iNaT
from pandas.core.dtypes.common import (
ensure_int64,
ensure_object,
Expand Down Expand Up @@ -9273,9 +9273,9 @@ def describe_categorical_1d(data):
tz = data.dt.tz
asint = data.dropna().values.view('i8')
names += ['top', 'freq', 'first', 'last']
result += [tslib.Timestamp(top, tz=tz), freq,
tslib.Timestamp(asint.min(), tz=tz),
tslib.Timestamp(asint.max(), tz=tz)]
result += [Timestamp(top, tz=tz), freq,
Timestamp(asint.min(), tz=tz),
Timestamp(asint.max(), tz=tz)]
else:
names += ['top', 'freq']
result += [top, freq]
Expand Down Expand Up @@ -10613,7 +10613,7 @@ def cum_func(self, axis=None, skipna=True, *args, **kwargs):
issubclass(y.dtype.type, (np.datetime64, np.timedelta64))):
result = accum_func(y, axis)
mask = isna(self)
np.putmask(result, mask, tslib.iNaT)
np.putmask(result, mask, iNaT)
elif skipna and not issubclass(y.dtype.type, (np.integer, np.bool_)):
mask = isna(self)
np.putmask(y, mask, mask_a)
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
)
from pandas.core.tools.datetimes import parse_time_string, DateParseError

from pandas._libs import tslib, index as libindex
from pandas._libs import index as libindex
from pandas._libs.tslibs.period import (Period, IncompatibleFrequency,
DIFFERENT_FREQ_INDEX)

from pandas._libs.tslibs import resolution
from pandas._libs.tslibs import resolution, NaT, iNaT

from pandas.core.algorithms import unique1d
import pandas.core.arrays.datetimelike as dtl
Expand Down Expand Up @@ -336,7 +336,7 @@ def _box_func(self):
# places outside of indexes/period.py are calling this _box_func,
# but passing data that's already boxed.
def func(x):
if isinstance(x, Period) or x is tslib.NaT:
if isinstance(x, Period) or x is NaT:
return x
else:
return Period._from_ordinal(ordinal=x, freq=self.freq)
Expand Down Expand Up @@ -726,7 +726,7 @@ def get_loc(self, key, method=None, tolerance=None):
raise KeyError(key)

try:
ordinal = tslib.iNaT if key is tslib.NaT else key.ordinal
ordinal = iNaT if key is NaT else key.ordinal
if tolerance is not None:
tolerance = self._convert_tolerance(tolerance,
np.asarray(key))
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import pytest

import pandas as pd
from pandas._libs import tslib, lib, missing as libmissing
from pandas._libs import lib, iNaT, missing as libmissing
from pandas import (Series, Index, DataFrame, Timedelta,
DatetimeIndex, TimedeltaIndex, Timestamp,
Panel, Period, Categorical, isna, Interval,
Expand Down Expand Up @@ -1263,7 +1263,7 @@ def test_nan_to_nat_conversions():
}))
df.iloc[3:6, :] = np.nan
result = df.loc[4, 'B'].value
assert (result == tslib.iNaT)
assert (result == iNaT)

s = df['B'].copy()
s._data = s._data.setitem(indexer=tuple([slice(8, 9)]), value=np.nan)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ def test_constructor_maskedarray_nonfloat(self):
assert 1 == frame['A'][1]
assert 2 == frame['C'][2]

# masked np.datetime64 stays (use lib.NaT as null)
# masked np.datetime64 stays (use NaT as null)
mat = ma.masked_all((2, 3), dtype='M8[ns]')
# 2-D input
frame = DataFrame(mat, columns=['A', 'B', 'C'], index=[1, 2])
Expand Down
18 changes: 9 additions & 9 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import pytz

from pandas._libs import tslib
from pandas._libs.tslibs import parsing
from pandas._libs.tslibs import iNaT, parsing
from pandas.compat import PY3, lmap
from pandas.errors import OutOfBoundsDatetime
import pandas.util._test_decorators as td
Expand Down Expand Up @@ -652,7 +652,7 @@ def test_unit(self, cache):
with pytest.raises(ValueError):
to_datetime([1], unit='D', format='%Y%m%d', cache=cache)

values = [11111111, 1, 1.0, tslib.iNaT, NaT, np.nan,
values = [11111111, 1, 1.0, iNaT, NaT, np.nan,
'NaT', '']
result = to_datetime(values, unit='D', errors='ignore', cache=cache)
expected = Index([11111111, Timestamp('1970-01-02'),
Expand All @@ -669,7 +669,7 @@ def test_unit(self, cache):
with pytest.raises(tslib.OutOfBoundsDatetime):
to_datetime(values, unit='D', errors='raise', cache=cache)

values = [1420043460000, tslib.iNaT, NaT, np.nan, 'NaT']
values = [1420043460000, iNaT, NaT, np.nan, 'NaT']

result = to_datetime(values, errors='ignore', unit='s', cache=cache)
expected = Index([1420043460000, NaT, NaT,
Expand Down Expand Up @@ -1104,7 +1104,7 @@ def test_string_na_nat_conversion(self, cache):
expected = np.empty(4, dtype='M8[ns]')
for i, val in enumerate(strings):
if isna(val):
expected[i] = tslib.iNaT
expected[i] = iNaT
else:
expected[i] = parse_date(val)

Expand Down Expand Up @@ -1145,7 +1145,7 @@ def test_string_na_nat_conversion(self, cache):
for i in range(5):
x = series[i]
if isna(x):
expected[i] = tslib.iNaT
expected[i] = iNaT
else:
expected[i] = to_datetime(x, cache=cache)

Expand Down Expand Up @@ -1420,10 +1420,10 @@ def test_parsers_nat(self):
result2 = to_datetime('NaT')
result3 = Timestamp('NaT')
result4 = DatetimeIndex(['NaT'])[0]
assert result1 is tslib.NaT
assert result2 is tslib.NaT
assert result3 is tslib.NaT
assert result4 is tslib.NaT
assert result1 is NaT
assert result2 is NaT
assert result3 is NaT
assert result4 is NaT

@pytest.mark.parametrize('cache', [True, False])
def test_parsers_dayfirst_yearfirst(self, cache):
Expand Down
46 changes: 22 additions & 24 deletions pandas/tests/indexes/period/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
import numpy as np
import pytest

import pandas._libs.tslib as tslib

import pandas as pd
from pandas import DatetimeIndex, Index, Period, PeriodIndex, Series
from pandas import DatetimeIndex, Index, NaT, Period, PeriodIndex, Series
from pandas.core.arrays import PeriodArray
from pandas.tests.test_base import Ops
import pandas.util.testing as tm
Expand All @@ -29,13 +27,13 @@ def test_ops_properties(self):
def test_minmax(self):

# monotonic
idx1 = pd.PeriodIndex([pd.NaT, '2011-01-01', '2011-01-02',
idx1 = pd.PeriodIndex([NaT, '2011-01-01', '2011-01-02',
'2011-01-03'], freq='D')
assert idx1.is_monotonic

# non-monotonic
idx2 = pd.PeriodIndex(['2011-01-01', pd.NaT, '2011-01-03',
'2011-01-02', pd.NaT], freq='D')
idx2 = pd.PeriodIndex(['2011-01-01', NaT, '2011-01-03',
'2011-01-02', NaT], freq='D')
assert not idx2.is_monotonic

for idx in [idx1, idx2]:
Expand All @@ -50,15 +48,15 @@ def test_minmax(self):
# Return NaT
obj = PeriodIndex([], freq='M')
result = getattr(obj, op)()
assert result is tslib.NaT
assert result is NaT

obj = PeriodIndex([pd.NaT], freq='M')
obj = PeriodIndex([NaT], freq='M')
result = getattr(obj, op)()
assert result is tslib.NaT
assert result is NaT

obj = PeriodIndex([pd.NaT, pd.NaT, pd.NaT], freq='M')
obj = PeriodIndex([NaT, NaT, NaT], freq='M')
result = getattr(obj, op)()
assert result is tslib.NaT
assert result is NaT

def test_numpy_minmax(self):
pr = pd.period_range(start='2016-01-15', end='2016-01-20')
Expand Down Expand Up @@ -113,7 +111,7 @@ def test_value_counts_unique(self):

idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 09:00',
'2013-01-01 09:00', '2013-01-01 08:00',
'2013-01-01 08:00', pd.NaT], freq='H')
'2013-01-01 08:00', NaT], freq='H')

exp_idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 08:00'],
freq='H')
Expand All @@ -123,7 +121,7 @@ def test_value_counts_unique(self):
tm.assert_series_equal(obj.value_counts(), expected)

exp_idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 08:00',
pd.NaT], freq='H')
NaT], freq='H')
expected = Series([3, 2, 1], index=exp_idx)

for obj in [idx, Series(idx)]:
Expand Down Expand Up @@ -284,9 +282,9 @@ def test_order(self):
'2011-01-03', '2011-01-05'],
freq='D', name='idx2')

idx3 = PeriodIndex([pd.NaT, '2011-01-03', '2011-01-05',
'2011-01-02', pd.NaT], freq='D', name='idx3')
exp3 = PeriodIndex([pd.NaT, pd.NaT, '2011-01-02', '2011-01-03',
idx3 = PeriodIndex([NaT, '2011-01-03', '2011-01-05',
'2011-01-02', NaT], freq='D', name='idx3')
exp3 = PeriodIndex([NaT, NaT, '2011-01-02', '2011-01-03',
'2011-01-05'], freq='D', name='idx3')

for idx, expected in [(idx1, exp1), (idx2, exp2), (idx3, exp3)]:
Expand Down Expand Up @@ -338,8 +336,8 @@ def test_repeat(self):
tm.assert_index_equal(res, exp)

def test_nat(self):
assert pd.PeriodIndex._na_value is pd.NaT
assert pd.PeriodIndex([], freq='M')._na_value is pd.NaT
assert pd.PeriodIndex._na_value is NaT
assert pd.PeriodIndex([], freq='M')._na_value is NaT

idx = pd.PeriodIndex(['2011-01-01', '2011-01-02'], freq='D')
assert idx._can_hold_na
Expand Down Expand Up @@ -460,10 +458,10 @@ def test_pi_comp_period_nat(self):
f = lambda x: pd.Period('2011-03', freq='M') == x
self._check(idx, f, exp)

f = lambda x: x == tslib.NaT
f = lambda x: x == NaT
exp = np.array([False, False, False, False], dtype=np.bool)
self._check(idx, f, exp)
f = lambda x: tslib.NaT == x
f = lambda x: NaT == x
self._check(idx, f, exp)

f = lambda x: x != pd.Period('2011-03', freq='M')
Expand All @@ -472,10 +470,10 @@ def test_pi_comp_period_nat(self):
f = lambda x: pd.Period('2011-03', freq='M') != x
self._check(idx, f, exp)

f = lambda x: x != tslib.NaT
f = lambda x: x != NaT
exp = np.array([True, True, True, True], dtype=np.bool)
self._check(idx, f, exp)
f = lambda x: tslib.NaT != x
f = lambda x: NaT != x
self._check(idx, f, exp)

f = lambda x: pd.Period('2011-03', freq='M') >= x
Expand All @@ -486,11 +484,11 @@ def test_pi_comp_period_nat(self):
exp = np.array([True, False, False, False], dtype=np.bool)
self._check(idx, f, exp)

f = lambda x: x > tslib.NaT
f = lambda x: x > NaT
exp = np.array([False, False, False, False], dtype=np.bool)
self._check(idx, f, exp)

f = lambda x: tslib.NaT >= x
f = lambda x: NaT >= x
exp = np.array([False, False, False, False], dtype=np.bool)
self._check(idx, f, exp)

Expand Down
Loading

0 comments on commit 712fa94

Please sign in to comment.