Skip to content

Commit d3a6a3a

Browse files
authored
BUG : Series.to_timestamp and Series.to_period raise user-facing AssertionError (#34067)
1 parent 45fee32 commit d3a6a3a

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

doc/source/whatsnew/v1.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ Backwards incompatible API changes
339339
- Combining a ``Categorical`` with integer categories and which contains missing values
340340
with a float dtype column in operations such as :func:`concat` or :meth:`~DataFrame.append`
341341
will now result in a float column instead of an object dtyped column (:issue:`33607`)
342+
- :meth:`Series.to_timestamp` now raises a ``TypeError`` if the axis is not a :class:`PeriodIndex`. Previously an ``AttributeError`` was raised (:issue:`33327`)
343+
- :meth:`Series.to_period` now raises a ``TypeError`` if the axis is not a :class:`DatetimeIndex`. Previously an ``AttributeError`` was raised (:issue:`33327`)
342344

343345
``MultiIndex.get_indexer`` interprets `method` argument differently
344346
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

pandas/core/series.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -4684,7 +4684,8 @@ def to_timestamp(self, freq=None, how="start", copy=True) -> "Series":
46844684
if copy:
46854685
new_values = new_values.copy()
46864686

4687-
assert isinstance(self.index, PeriodIndex)
4687+
if not isinstance(self.index, PeriodIndex):
4688+
raise TypeError(f"unsupported Type {type(self.index).__name__}")
46884689
new_index = self.index.to_timestamp(freq=freq, how=how) # type: ignore
46894690
return self._constructor(new_values, index=new_index).__finalize__(
46904691
self, method="to_timestamp"
@@ -4711,7 +4712,8 @@ def to_period(self, freq=None, copy=True) -> "Series":
47114712
if copy:
47124713
new_values = new_values.copy()
47134714

4714-
assert isinstance(self.index, DatetimeIndex)
4715+
if not isinstance(self.index, DatetimeIndex):
4716+
raise TypeError(f"unsupported Type {type(self.index).__name__}")
47154717
new_index = self.index.to_period(freq=freq) # type: ignore
47164718
return self._constructor(new_values, index=new_index).__finalize__(
47174719
self, method="to_period"

pandas/tests/series/methods/test_to_period.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import pytest
23

34
from pandas import (
45
DataFrame,
@@ -45,3 +46,12 @@ def test_to_period(self):
4546
expected = df.copy()
4647
expected.columns = exp_idx
4748
tm.assert_frame_equal(df.to_period(axis=1), expected)
49+
50+
def test_to_period_raises(self, indices):
51+
# https://github.com/pandas-dev/pandas/issues/33327
52+
index = indices
53+
ser = Series(index=index, dtype=object)
54+
if not isinstance(index, DatetimeIndex):
55+
msg = f"unsupported Type {type(index).__name__}"
56+
with pytest.raises(TypeError, match=msg):
57+
ser.to_period()

pandas/tests/series/methods/test_to_timestamp.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from datetime import timedelta
22

3-
from pandas import Series, Timedelta, date_range, period_range, to_datetime
3+
import pytest
4+
5+
from pandas import PeriodIndex, Series, Timedelta, date_range, period_range, to_datetime
46
import pandas._testing as tm
57

68

@@ -52,3 +54,12 @@ def _get_with_delta(delta, freq="A-DEC"):
5254
exp_index = exp_index + Timedelta(1, "s") - Timedelta(1, "ns")
5355
tm.assert_index_equal(result.index, exp_index)
5456
assert result.name == "foo"
57+
58+
def test_to_timestamp_raises(self, indices):
59+
# https://github.com/pandas-dev/pandas/issues/33327
60+
index = indices
61+
ser = Series(index=index, dtype=object)
62+
if not isinstance(index, PeriodIndex):
63+
msg = f"unsupported Type {type(index).__name__}"
64+
with pytest.raises(TypeError, match=msg):
65+
ser.to_timestamp()

0 commit comments

Comments
 (0)