Skip to content

Commit 18025cf

Browse files
author
tp
committed
Add tests for TypeError when index isn't a DatetimeIndex
1 parent 295323c commit 18025cf

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

doc/source/whatsnew/v0.23.0.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -916,10 +916,10 @@ Datetimelike API Changes
916916
- :class:`CacheableOffset` and :class:`WeekDay` are no longer available in the ``pandas.tseries.offsets`` module (:issue:`17830`)
917917
- ``pandas.tseries.frequencies.get_freq_group()`` and ``pandas.tseries.frequencies.DAYS`` are removed from the public API (:issue:`18034`)
918918
- :func:`Series.truncate` and :func:`DataFrame.truncate` will raise a ``ValueError`` if the index is not sorted instead of an unhelpful ``KeyError`` (:issue:`17935`)
919-
- :attr:`Series.at_time` and :attr:`DataFrame.at_time` will now raise a ``TypeError``
919+
- :attr:`Series.first` and :attr:`DataFrame.first` will now raise a ``TypeError``
920+
rather than ``NotImplementedError`` when index is not a :class:`DatetimeIndex`` (:issue:`20725`).
921+
- :attr:`Series.last` and :attr:`DateFrame.last` will now raise a ``TypeError``
920922
rather than ``NotImplementedError`` when index is not a :class:`DatetimeIndex`` (:issue:`20725`).
921-
- :attr:`Series.between_time` and :attr:`DateFrame.between_time` will now raise
922-
a ``TypeError`` rather than ``NotImplementedError`` when index is not a :class:`DatetimeIndex`` (:issue:`20725`).
923923
- Restricted ``DateOffset`` keyword arguments. Previously, ``DateOffset`` subclasses allowed arbitrary keyword arguments which could lead to unexpected behavior. Now, only valid arguments will be accepted. (:issue:`17176`, :issue:`18226`).
924924
- :func:`pandas.merge` provides a more informative error message when trying to merge on timezone-aware and timezone-naive columns (:issue:`15800`)
925925
- For :class:`DatetimeIndex` and :class:`TimedeltaIndex` with ``freq=None``, addition or subtraction of integer-dtyped array or ``Index`` will raise ``NullFrequencyError`` instead of ``TypeError`` (:issue:`19895`)

pandas/tests/frame/test_timeseries.py

+66-2
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,59 @@ def test_first_last_valid(self):
539539
assert frame.first_valid_index().freq == frame.index.freq
540540
assert frame.last_valid_index().freq == frame.index.freq
541541

542-
def test_at_time_frame(self):
542+
def test_first_subset(self):
543+
ts = tm.makeTimeDataFrame(freq='12h')
544+
result = ts.first('10d')
545+
assert len(result) == 20
546+
547+
ts = tm.makeTimeDataFrame(freq='D')
548+
result = ts.first('10d')
549+
assert len(result) == 10
550+
551+
result = ts.first('3M')
552+
expected = ts[:'3/31/2000']
553+
assert_frame_equal(result, expected)
554+
555+
result = ts.first('21D')
556+
expected = ts[:21]
557+
assert_frame_equal(result, expected)
558+
559+
result = ts[:0].first('3M')
560+
assert_frame_equal(result, ts[:0])
561+
562+
def test_first_raises(self):
563+
# GH20725
564+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]])
565+
with pytest.raises(TypeError): # index is not a DatetimeIndex
566+
df.first('1D')
567+
568+
def test_last_subset(self):
569+
ts = tm.makeTimeDataFrame(freq='12h')
570+
result = ts.last('10d')
571+
assert len(result) == 20
572+
573+
ts = tm.makeTimeDataFrame(nper=30, freq='D')
574+
result = ts.last('10d')
575+
assert len(result) == 10
576+
577+
result = ts.last('21D')
578+
expected = ts['2000-01-10':]
579+
assert_frame_equal(result, expected)
580+
581+
result = ts.last('21D')
582+
expected = ts[-21:]
583+
assert_frame_equal(result, expected)
584+
585+
result = ts[:0].last('3M')
586+
assert_frame_equal(result, ts[:0])
587+
588+
def test_last_raises(self):
589+
# GH20725
590+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]])
591+
with pytest.raises(TypeError): # index is not a DatetimeIndex
592+
df.last('1D')
593+
594+
def test_at_time(self):
543595
rng = date_range('1/1/2000', '1/5/2000', freq='5min')
544596
ts = DataFrame(np.random.randn(len(rng), 2), index=rng)
545597
rs = ts.at_time(rng[1])
@@ -569,7 +621,13 @@ def test_at_time_frame(self):
569621
rs = ts.at_time('16:00')
570622
assert len(rs) == 0
571623

572-
def test_between_time_frame(self):
624+
def test_at_time_raises(self):
625+
# GH20725
626+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]])
627+
with pytest.raises(TypeError): # index is not a DatetimeIndex
628+
df.at_time('00:00')
629+
630+
def test_between_time(self):
573631
rng = date_range('1/1/2000', '1/5/2000', freq='5min')
574632
ts = DataFrame(np.random.randn(len(rng), 2), index=rng)
575633
stime = time(0, 0)
@@ -629,6 +687,12 @@ def test_between_time_frame(self):
629687
else:
630688
assert (t < etime) or (t >= stime)
631689

690+
def test_between_time_raises(self):
691+
# GH20725
692+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]])
693+
with pytest.raises(TypeError): # index is not a DatetimeIndex
694+
df.between_time(start_time='00:00', end_time='12:00')
695+
632696
def test_operation_on_NaT(self):
633697
# Both NaT and Timestamp are in DataFrame.
634698
df = pd.DataFrame({'foo': [pd.NaT, pd.NaT,

pandas/tests/series/test_timeseries.py

+24
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,12 @@ def test_first_subset(self):
628628
result = ts[:0].first('3M')
629629
assert_series_equal(result, ts[:0])
630630

631+
def test_first_raises(self):
632+
# GH20725
633+
ser = pd.Series('a b c'.split())
634+
with pytest.raises(TypeError): # index is not a DatetimeIndex
635+
ser.first('1D')
636+
631637
def test_last_subset(self):
632638
ts = _simple_ts('1/1/2000', '1/1/2010', freq='12h')
633639
result = ts.last('10d')
@@ -648,6 +654,12 @@ def test_last_subset(self):
648654
result = ts[:0].last('3M')
649655
assert_series_equal(result, ts[:0])
650656

657+
def test_last_raises(self):
658+
# GH20725
659+
ser = pd.Series('a b c'.split())
660+
with pytest.raises(TypeError): # index is not a DatetimeIndex
661+
ser.last('1D')
662+
651663
def test_format_pre_1900_dates(self):
652664
rng = date_range('1/1/1850', '1/1/1950', freq='A-DEC')
653665
rng.format()
@@ -696,6 +708,12 @@ def test_at_time(self):
696708
rs = ts.at_time('16:00')
697709
assert len(rs) == 0
698710

711+
def test_at_time_raises(self):
712+
# GH20725
713+
ser = pd.Series('a b c'.split())
714+
with pytest.raises(TypeError): # index is not a DatetimeIndex
715+
ser.at_time('00:00')
716+
699717
def test_between(self):
700718
series = Series(date_range('1/1/2000', periods=10))
701719
left, right = series[[2, 7]]
@@ -764,6 +782,12 @@ def test_between_time(self):
764782
else:
765783
assert (t < etime) or (t >= stime)
766784

785+
def test_between_time_raises(self):
786+
# GH20725
787+
ser = pd.Series('a b c'.split())
788+
with pytest.raises(TypeError): # index is not a DatetimeIndex
789+
ser.between_time(start_time='00:00', end_time='12:00')
790+
767791
def test_between_time_types(self):
768792
# GH11818
769793
rng = date_range('1/1/2000', '1/5/2000', freq='5min')

0 commit comments

Comments
 (0)