Skip to content

Commit 804fb99

Browse files
jbrockmendeljreback
authored andcommitted
Index tests in the wrong places (#18074)
1 parent 0fd3bd7 commit 804fb99

File tree

7 files changed

+191
-201
lines changed

7 files changed

+191
-201
lines changed

pandas/tests/indexes/datetimes/test_astype.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ def test_astype_datetime64(self):
117117
dtype='datetime64[ns]')
118118
tm.assert_index_equal(result, expected)
119119

120+
def test_astype_object(self):
121+
rng = date_range('1/1/2000', periods=20)
122+
123+
casted = rng.astype('O')
124+
exp_values = list(rng)
125+
126+
tm.assert_index_equal(casted, Index(exp_values, dtype=np.object_))
127+
assert casted.tolist() == exp_values
128+
120129
def test_astype_raises(self):
121130
# GH 13149, GH 13209
122131
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
@@ -287,12 +296,18 @@ def test_to_period_tz_dateutil(self):
287296
assert result == expected
288297
tm.assert_index_equal(ts.to_period(), xp)
289298

290-
def test_astype_object(self):
291-
# NumPy 1.6.1 weak ns support
292-
rng = date_range('1/1/2000', periods=20)
293-
294-
casted = rng.astype('O')
295-
exp_values = list(rng)
296-
297-
tm.assert_index_equal(casted, Index(exp_values, dtype=np.object_))
298-
assert casted.tolist() == exp_values
299+
def test_to_period_nofreq(self):
300+
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04'])
301+
pytest.raises(ValueError, idx.to_period)
302+
303+
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'],
304+
freq='infer')
305+
assert idx.freqstr == 'D'
306+
expected = pd.PeriodIndex(['2000-01-01', '2000-01-02',
307+
'2000-01-03'], freq='D')
308+
tm.assert_index_equal(idx.to_period(), expected)
309+
310+
# GH 7606
311+
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'])
312+
assert idx.freqstr is None
313+
tm.assert_index_equal(idx.to_period(), expected)

pandas/tests/indexes/datetimes/test_datetime.py

+2-81
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import pandas.util.testing as tm
99
from pandas.compat import lrange
1010
from pandas.compat.numpy import np_datetime64_compat
11-
from pandas import (DatetimeIndex, Index, date_range, Series, DataFrame,
11+
from pandas import (DatetimeIndex, Index, date_range, DataFrame,
1212
Timestamp, datetime, offsets)
1313

14-
from pandas.util.testing import assert_series_equal, assert_almost_equal
14+
from pandas.util.testing import assert_almost_equal
1515

1616
randn = np.random.randn
1717

@@ -223,22 +223,6 @@ def test_append_join_nondatetimeindex(self):
223223
# it works
224224
rng.join(idx, how='outer')
225225

226-
def test_to_period_nofreq(self):
227-
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04'])
228-
pytest.raises(ValueError, idx.to_period)
229-
230-
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'],
231-
freq='infer')
232-
assert idx.freqstr == 'D'
233-
expected = pd.PeriodIndex(['2000-01-01', '2000-01-02',
234-
'2000-01-03'], freq='D')
235-
tm.assert_index_equal(idx.to_period(), expected)
236-
237-
# GH 7606
238-
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'])
239-
assert idx.freqstr is None
240-
tm.assert_index_equal(idx.to_period(), expected)
241-
242226
def test_comparisons_coverage(self):
243227
rng = date_range('1/1/2000', periods=10)
244228

@@ -567,13 +551,6 @@ def test_does_not_convert_mixed_integer(self):
567551
assert cols.dtype == joined.dtype
568552
tm.assert_numpy_array_equal(cols.values, joined.values)
569553

570-
def test_slice_keeps_name(self):
571-
# GH4226
572-
st = pd.Timestamp('2013-07-01 00:00:00', tz='America/Los_Angeles')
573-
et = pd.Timestamp('2013-07-02 00:00:00', tz='America/Los_Angeles')
574-
dr = pd.date_range(st, et, freq='H', name='timebucket')
575-
assert dr[1:].name == dr.name
576-
577554
def test_join_self(self):
578555
index = date_range('1/1/2000', periods=10)
579556
kinds = 'outer', 'inner', 'left', 'right'
@@ -687,59 +664,3 @@ def test_factorize_dst(self):
687664
arr, res = obj.factorize()
688665
tm.assert_numpy_array_equal(arr, np.arange(12, dtype=np.intp))
689666
tm.assert_index_equal(res, idx)
690-
691-
def test_slice_with_negative_step(self):
692-
ts = Series(np.arange(20),
693-
date_range('2014-01-01', periods=20, freq='MS'))
694-
SLC = pd.IndexSlice
695-
696-
def assert_slices_equivalent(l_slc, i_slc):
697-
assert_series_equal(ts[l_slc], ts.iloc[i_slc])
698-
assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
699-
assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
700-
701-
assert_slices_equivalent(SLC[Timestamp('2014-10-01')::-1], SLC[9::-1])
702-
assert_slices_equivalent(SLC['2014-10-01'::-1], SLC[9::-1])
703-
704-
assert_slices_equivalent(SLC[:Timestamp('2014-10-01'):-1], SLC[:8:-1])
705-
assert_slices_equivalent(SLC[:'2014-10-01':-1], SLC[:8:-1])
706-
707-
assert_slices_equivalent(SLC['2015-02-01':'2014-10-01':-1],
708-
SLC[13:8:-1])
709-
assert_slices_equivalent(SLC[Timestamp('2015-02-01'):Timestamp(
710-
'2014-10-01'):-1], SLC[13:8:-1])
711-
assert_slices_equivalent(SLC['2015-02-01':Timestamp('2014-10-01'):-1],
712-
SLC[13:8:-1])
713-
assert_slices_equivalent(SLC[Timestamp('2015-02-01'):'2014-10-01':-1],
714-
SLC[13:8:-1])
715-
716-
assert_slices_equivalent(SLC['2014-10-01':'2015-02-01':-1], SLC[:0])
717-
718-
def test_slice_with_zero_step_raises(self):
719-
ts = Series(np.arange(20),
720-
date_range('2014-01-01', periods=20, freq='MS'))
721-
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
722-
lambda: ts[::0])
723-
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
724-
lambda: ts.loc[::0])
725-
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
726-
lambda: ts.loc[::0])
727-
728-
def test_slice_bounds_empty(self):
729-
# GH 14354
730-
empty_idx = DatetimeIndex(freq='1H', periods=0, end='2015')
731-
732-
right = empty_idx._maybe_cast_slice_bound('2015-01-02', 'right', 'loc')
733-
exp = Timestamp('2015-01-02 23:59:59.999999999')
734-
assert right == exp
735-
736-
left = empty_idx._maybe_cast_slice_bound('2015-01-02', 'left', 'loc')
737-
exp = Timestamp('2015-01-02 00:00:00')
738-
assert left == exp
739-
740-
def test_slice_duplicate_monotonic(self):
741-
# https://github.com/pandas-dev/pandas/issues/16515
742-
idx = pd.DatetimeIndex(['2017', '2017'])
743-
result = idx._maybe_cast_slice_bound('2017-01-01', 'left', 'loc')
744-
expected = Timestamp('2017-01-01')
745-
assert result == expected

pandas/tests/indexes/datetimes/test_ops.py

-43
Original file line numberDiff line numberDiff line change
@@ -383,49 +383,6 @@ def test_resolution(self):
383383
tz=tz)
384384
assert idx.resolution == expected
385385

386-
def test_union(self):
387-
for tz in self.tz:
388-
# union
389-
rng1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
390-
other1 = pd.date_range('1/6/2000', freq='D', periods=5, tz=tz)
391-
expected1 = pd.date_range('1/1/2000', freq='D', periods=10, tz=tz)
392-
393-
rng2 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
394-
other2 = pd.date_range('1/4/2000', freq='D', periods=5, tz=tz)
395-
expected2 = pd.date_range('1/1/2000', freq='D', periods=8, tz=tz)
396-
397-
rng3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
398-
other3 = pd.DatetimeIndex([], tz=tz)
399-
expected3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
400-
401-
for rng, other, expected in [(rng1, other1, expected1),
402-
(rng2, other2, expected2),
403-
(rng3, other3, expected3)]:
404-
405-
result_union = rng.union(other)
406-
tm.assert_index_equal(result_union, expected)
407-
408-
def test_difference(self):
409-
for tz in self.tz:
410-
# diff
411-
rng1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
412-
other1 = pd.date_range('1/6/2000', freq='D', periods=5, tz=tz)
413-
expected1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
414-
415-
rng2 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
416-
other2 = pd.date_range('1/4/2000', freq='D', periods=5, tz=tz)
417-
expected2 = pd.date_range('1/1/2000', freq='D', periods=3, tz=tz)
418-
419-
rng3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
420-
other3 = pd.DatetimeIndex([], tz=tz)
421-
expected3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
422-
423-
for rng, other, expected in [(rng1, other1, expected1),
424-
(rng2, other2, expected2),
425-
(rng3, other3, expected3)]:
426-
result_diff = rng.difference(other)
427-
tm.assert_index_equal(result_diff, expected)
428-
429386
def test_comp_nat(self):
430387
left = pd.DatetimeIndex([pd.Timestamp('2011-01-01'), pd.NaT,
431388
pd.Timestamp('2011-01-03')])

pandas/tests/indexes/datetimes/test_partial_slicing.py

+62
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,68 @@
1212

1313

1414
class TestSlicing(object):
15+
def test_slice_keeps_name(self):
16+
# GH4226
17+
st = pd.Timestamp('2013-07-01 00:00:00', tz='America/Los_Angeles')
18+
et = pd.Timestamp('2013-07-02 00:00:00', tz='America/Los_Angeles')
19+
dr = pd.date_range(st, et, freq='H', name='timebucket')
20+
assert dr[1:].name == dr.name
21+
22+
def test_slice_with_negative_step(self):
23+
ts = Series(np.arange(20),
24+
date_range('2014-01-01', periods=20, freq='MS'))
25+
SLC = pd.IndexSlice
26+
27+
def assert_slices_equivalent(l_slc, i_slc):
28+
tm.assert_series_equal(ts[l_slc], ts.iloc[i_slc])
29+
tm.assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
30+
tm.assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
31+
32+
assert_slices_equivalent(SLC[Timestamp('2014-10-01')::-1], SLC[9::-1])
33+
assert_slices_equivalent(SLC['2014-10-01'::-1], SLC[9::-1])
34+
35+
assert_slices_equivalent(SLC[:Timestamp('2014-10-01'):-1], SLC[:8:-1])
36+
assert_slices_equivalent(SLC[:'2014-10-01':-1], SLC[:8:-1])
37+
38+
assert_slices_equivalent(SLC['2015-02-01':'2014-10-01':-1],
39+
SLC[13:8:-1])
40+
assert_slices_equivalent(SLC[Timestamp('2015-02-01'):Timestamp(
41+
'2014-10-01'):-1], SLC[13:8:-1])
42+
assert_slices_equivalent(SLC['2015-02-01':Timestamp('2014-10-01'):-1],
43+
SLC[13:8:-1])
44+
assert_slices_equivalent(SLC[Timestamp('2015-02-01'):'2014-10-01':-1],
45+
SLC[13:8:-1])
46+
47+
assert_slices_equivalent(SLC['2014-10-01':'2015-02-01':-1], SLC[:0])
48+
49+
def test_slice_with_zero_step_raises(self):
50+
ts = Series(np.arange(20),
51+
date_range('2014-01-01', periods=20, freq='MS'))
52+
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
53+
lambda: ts[::0])
54+
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
55+
lambda: ts.loc[::0])
56+
tm.assert_raises_regex(ValueError, 'slice step cannot be zero',
57+
lambda: ts.loc[::0])
58+
59+
def test_slice_bounds_empty(self):
60+
# GH 14354
61+
empty_idx = DatetimeIndex(freq='1H', periods=0, end='2015')
62+
63+
right = empty_idx._maybe_cast_slice_bound('2015-01-02', 'right', 'loc')
64+
exp = Timestamp('2015-01-02 23:59:59.999999999')
65+
assert right == exp
66+
67+
left = empty_idx._maybe_cast_slice_bound('2015-01-02', 'left', 'loc')
68+
exp = Timestamp('2015-01-02 00:00:00')
69+
assert left == exp
70+
71+
def test_slice_duplicate_monotonic(self):
72+
# https://github.com/pandas-dev/pandas/issues/16515
73+
idx = pd.DatetimeIndex(['2017', '2017'])
74+
result = idx._maybe_cast_slice_bound('2017-01-01', 'left', 'loc')
75+
expected = Timestamp('2017-01-01')
76+
assert result == expected
1577

1678
def test_slice_year(self):
1779
dti = DatetimeIndex(freq='B', start=datetime(2005, 1, 1), periods=500)

0 commit comments

Comments
 (0)