Skip to content

Commit 712fa94

Browse files
jbrockmendeljreback
authored andcommitted
remove uses of (ts)?lib.(NaT|iNaT|Timestamp) (#23562)
1 parent 8212001 commit 712fa94

File tree

12 files changed

+138
-149
lines changed

12 files changed

+138
-149
lines changed

pandas/core/generic.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import numpy as np
1111
import pandas as pd
1212

13-
from pandas._libs import tslib, properties
13+
from pandas._libs import properties, Timestamp, iNaT
1414
from pandas.core.dtypes.common import (
1515
ensure_int64,
1616
ensure_object,
@@ -9273,9 +9273,9 @@ def describe_categorical_1d(data):
92739273
tz = data.dt.tz
92749274
asint = data.dropna().values.view('i8')
92759275
names += ['top', 'freq', 'first', 'last']
9276-
result += [tslib.Timestamp(top, tz=tz), freq,
9277-
tslib.Timestamp(asint.min(), tz=tz),
9278-
tslib.Timestamp(asint.max(), tz=tz)]
9276+
result += [Timestamp(top, tz=tz), freq,
9277+
Timestamp(asint.min(), tz=tz),
9278+
Timestamp(asint.max(), tz=tz)]
92799279
else:
92809280
names += ['top', 'freq']
92819281
result += [top, freq]
@@ -10613,7 +10613,7 @@ def cum_func(self, axis=None, skipna=True, *args, **kwargs):
1061310613
issubclass(y.dtype.type, (np.datetime64, np.timedelta64))):
1061410614
result = accum_func(y, axis)
1061510615
mask = isna(self)
10616-
np.putmask(result, mask, tslib.iNaT)
10616+
np.putmask(result, mask, iNaT)
1061710617
elif skipna and not issubclass(y.dtype.type, (np.integer, np.bool_)):
1061810618
mask = isna(self)
1061910619
np.putmask(y, mask, mask_a)

pandas/core/indexes/period.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
)
2323
from pandas.core.tools.datetimes import parse_time_string, DateParseError
2424

25-
from pandas._libs import tslib, index as libindex
25+
from pandas._libs import index as libindex
2626
from pandas._libs.tslibs.period import (Period, IncompatibleFrequency,
2727
DIFFERENT_FREQ_INDEX)
2828

29-
from pandas._libs.tslibs import resolution
29+
from pandas._libs.tslibs import resolution, NaT, iNaT
3030

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

728728
try:
729-
ordinal = tslib.iNaT if key is tslib.NaT else key.ordinal
729+
ordinal = iNaT if key is NaT else key.ordinal
730730
if tolerance is not None:
731731
tolerance = self._convert_tolerance(tolerance,
732732
np.asarray(key))

pandas/tests/dtypes/test_inference.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pytest
1616

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

12681268
s = df['B'].copy()
12691269
s._data = s._data.setitem(indexer=tuple([slice(8, 9)]), value=np.nan)

pandas/tests/frame/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ def test_constructor_maskedarray_nonfloat(self):
711711
assert 1 == frame['A'][1]
712712
assert 2 == frame['C'][2]
713713

714-
# masked np.datetime64 stays (use lib.NaT as null)
714+
# masked np.datetime64 stays (use NaT as null)
715715
mat = ma.masked_all((2, 3), dtype='M8[ns]')
716716
# 2-D input
717717
frame = DataFrame(mat, columns=['A', 'B', 'C'], index=[1, 2])

pandas/tests/indexes/datetimes/test_tools.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import pytz
1414

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

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

672-
values = [1420043460000, tslib.iNaT, NaT, np.nan, 'NaT']
672+
values = [1420043460000, iNaT, NaT, np.nan, 'NaT']
673673

674674
result = to_datetime(values, errors='ignore', unit='s', cache=cache)
675675
expected = Index([1420043460000, NaT, NaT,
@@ -1104,7 +1104,7 @@ def test_string_na_nat_conversion(self, cache):
11041104
expected = np.empty(4, dtype='M8[ns]')
11051105
for i, val in enumerate(strings):
11061106
if isna(val):
1107-
expected[i] = tslib.iNaT
1107+
expected[i] = iNaT
11081108
else:
11091109
expected[i] = parse_date(val)
11101110

@@ -1145,7 +1145,7 @@ def test_string_na_nat_conversion(self, cache):
11451145
for i in range(5):
11461146
x = series[i]
11471147
if isna(x):
1148-
expected[i] = tslib.iNaT
1148+
expected[i] = iNaT
11491149
else:
11501150
expected[i] = to_datetime(x, cache=cache)
11511151

@@ -1420,10 +1420,10 @@ def test_parsers_nat(self):
14201420
result2 = to_datetime('NaT')
14211421
result3 = Timestamp('NaT')
14221422
result4 = DatetimeIndex(['NaT'])[0]
1423-
assert result1 is tslib.NaT
1424-
assert result2 is tslib.NaT
1425-
assert result3 is tslib.NaT
1426-
assert result4 is tslib.NaT
1423+
assert result1 is NaT
1424+
assert result2 is NaT
1425+
assert result3 is NaT
1426+
assert result4 is NaT
14271427

14281428
@pytest.mark.parametrize('cache', [True, False])
14291429
def test_parsers_dayfirst_yearfirst(self, cache):

pandas/tests/indexes/period/test_ops.py

+22-24
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
import numpy as np
33
import pytest
44

5-
import pandas._libs.tslib as tslib
6-
75
import pandas as pd
8-
from pandas import DatetimeIndex, Index, Period, PeriodIndex, Series
6+
from pandas import DatetimeIndex, Index, NaT, Period, PeriodIndex, Series
97
from pandas.core.arrays import PeriodArray
108
from pandas.tests.test_base import Ops
119
import pandas.util.testing as tm
@@ -29,13 +27,13 @@ def test_ops_properties(self):
2927
def test_minmax(self):
3028

3129
# monotonic
32-
idx1 = pd.PeriodIndex([pd.NaT, '2011-01-01', '2011-01-02',
30+
idx1 = pd.PeriodIndex([NaT, '2011-01-01', '2011-01-02',
3331
'2011-01-03'], freq='D')
3432
assert idx1.is_monotonic
3533

3634
# non-monotonic
37-
idx2 = pd.PeriodIndex(['2011-01-01', pd.NaT, '2011-01-03',
38-
'2011-01-02', pd.NaT], freq='D')
35+
idx2 = pd.PeriodIndex(['2011-01-01', NaT, '2011-01-03',
36+
'2011-01-02', NaT], freq='D')
3937
assert not idx2.is_monotonic
4038

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

55-
obj = PeriodIndex([pd.NaT], freq='M')
53+
obj = PeriodIndex([NaT], freq='M')
5654
result = getattr(obj, op)()
57-
assert result is tslib.NaT
55+
assert result is NaT
5856

59-
obj = PeriodIndex([pd.NaT, pd.NaT, pd.NaT], freq='M')
57+
obj = PeriodIndex([NaT, NaT, NaT], freq='M')
6058
result = getattr(obj, op)()
61-
assert result is tslib.NaT
59+
assert result is NaT
6260

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

114112
idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 09:00',
115113
'2013-01-01 09:00', '2013-01-01 08:00',
116-
'2013-01-01 08:00', pd.NaT], freq='H')
114+
'2013-01-01 08:00', NaT], freq='H')
117115

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

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

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

287-
idx3 = PeriodIndex([pd.NaT, '2011-01-03', '2011-01-05',
288-
'2011-01-02', pd.NaT], freq='D', name='idx3')
289-
exp3 = PeriodIndex([pd.NaT, pd.NaT, '2011-01-02', '2011-01-03',
285+
idx3 = PeriodIndex([NaT, '2011-01-03', '2011-01-05',
286+
'2011-01-02', NaT], freq='D', name='idx3')
287+
exp3 = PeriodIndex([NaT, NaT, '2011-01-02', '2011-01-03',
290288
'2011-01-05'], freq='D', name='idx3')
291289

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

340338
def test_nat(self):
341-
assert pd.PeriodIndex._na_value is pd.NaT
342-
assert pd.PeriodIndex([], freq='M')._na_value is pd.NaT
339+
assert pd.PeriodIndex._na_value is NaT
340+
assert pd.PeriodIndex([], freq='M')._na_value is NaT
343341

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

463-
f = lambda x: x == tslib.NaT
461+
f = lambda x: x == NaT
464462
exp = np.array([False, False, False, False], dtype=np.bool)
465463
self._check(idx, f, exp)
466-
f = lambda x: tslib.NaT == x
464+
f = lambda x: NaT == x
467465
self._check(idx, f, exp)
468466

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

475-
f = lambda x: x != tslib.NaT
473+
f = lambda x: x != NaT
476474
exp = np.array([True, True, True, True], dtype=np.bool)
477475
self._check(idx, f, exp)
478-
f = lambda x: tslib.NaT != x
476+
f = lambda x: NaT != x
479477
self._check(idx, f, exp)
480478

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

489-
f = lambda x: x > tslib.NaT
487+
f = lambda x: x > NaT
490488
exp = np.array([False, False, False, False], dtype=np.bool)
491489
self._check(idx, f, exp)
492490

493-
f = lambda x: tslib.NaT >= x
491+
f = lambda x: NaT >= x
494492
exp = np.array([False, False, False, False], dtype=np.bool)
495493
self._check(idx, f, exp)
496494

0 commit comments

Comments
 (0)