Skip to content

BUG : Series.to_timestamp and Series.to_period raise user-facing AssertionError #34067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 20, 2020
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ Backwards incompatible API changes
- Combining a ``Categorical`` with integer categories and which contains missing values
with a float dtype column in operations such as :func:`concat` or :meth:`~DataFrame.append`
will now result in a float column instead of an object dtyped column (:issue:`33607`)
- :meth:`Series.to_timestamp` now raises a ``TypeError`` if the axis is not a :class:`PeriodIndex`.
Previously an ``AttributeError`` was raised (:issue:`33327`)
- :meth:`Series.to_period` now raises a ``TypeError`` if the axis is not a :class:`DatetimeIndex`.
Previously an ``AttributeError`` was raised (:issue:`33327`)

``MultiIndex.get_indexer`` interprets `method` argument differently
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4684,7 +4684,8 @@ def to_timestamp(self, freq=None, how="start", copy=True) -> "Series":
if copy:
new_values = new_values.copy()

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

assert isinstance(self.index, DatetimeIndex)
if not isinstance(self.index, DatetimeIndex):
raise TypeError(f"unsupported Type {self.index}")
new_index = self.index.to_period(freq=freq) # type: ignore
return self._constructor(new_values, index=new_index).__finalize__(
self, method="to_period"
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/methods/test_to_period.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest

from pandas import (
DataFrame,
Expand Down Expand Up @@ -45,3 +46,10 @@ def test_to_period(self):
expected = df.copy()
expected.columns = exp_idx
tm.assert_frame_equal(df.to_period(axis=1), expected)

def test_to_period_raises(self, indices):
# invalid type , #34067 test
idx = indices
msg = "unsupported Type RangeIndex"
with pytest.raises(TypeError, match=msg):
Series([idx]).to_period()
18 changes: 18 additions & 0 deletions pandas/tests/series/methods/test_to_timestamp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from datetime import timedelta

import numpy as np
import pytest

from pandas import Series, Timedelta, date_range, period_range, to_datetime
import pandas._testing as tm

Expand Down Expand Up @@ -52,3 +55,18 @@ def _get_with_delta(delta, freq="A-DEC"):
exp_index = exp_index + Timedelta(1, "s") - Timedelta(1, "ns")
tm.assert_index_equal(result.index, exp_index)
assert result.name == "foo"

def test_to_timestamp_raises_type_error_for_rangeindex(self, indices):
# invalid type , #34067 test
idx = indices
msg = "unsupported Type RangeIndex"
with pytest.raises(TypeError, match=msg):
Series([idx]).to_timestamp()

def test_to_timestamp_raises_type_error_for_datetimeindex(self):
# invalid type , #34067 test
msg = "unsupported Type .*"
with pytest.raises(TypeError, match=msg):
rng = date_range("1/1/2012", periods=5, freq="M")
ts = Series(np.random.randn(len(rng)), index=rng)
ts.to_timestamp()