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
6 changes: 4 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4687,7 +4687,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 @@ -4714,7 +4715,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
5 changes: 5 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,7 @@ def test_to_period(self):
expected = df.copy()
expected.columns = exp_idx
tm.assert_frame_equal(df.to_period(axis=1), expected)

# invalid type , #34067 test
with pytest.raises(TypeError):
Series([0]).to_period()
11 changes: 11 additions & 0 deletions pandas/tests/series/methods/test_to_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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


class TestToTimestamp:
Expand Down Expand Up @@ -52,3 +54,12 @@ 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"

# invalid type , #34067 test
with pytest.raises(TypeError):
Series([0]).to_timestamp()

with pytest.raises(TypeError):
rng = date_range("1/1/2012", periods=5, freq="M")
ts = Series(np.random.randn(len(rng)), index=rng)
ts.to_timestamp()