Skip to content

BUG: User-facing AssertionError in Series.to_timestamp #33327

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

Closed
3 tasks done
TomAugspurger opened this issue Apr 6, 2020 · 10 comments · Fixed by #34067
Closed
3 tasks done

BUG: User-facing AssertionError in Series.to_timestamp #33327

TomAugspurger opened this issue Apr 6, 2020 · 10 comments · Fixed by #34067
Assignees
Labels
Bug Datetime Datetime data dtype Error Reporting Incorrect or improved errors from pandas good first issue Period Period data type
Milestone

Comments

@TomAugspurger
Copy link
Contributor

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

In [5]: import pandas as pd

In [6]: pd.Series([0]).to_timestamp()
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-6-1a9d08d7daba> in <module>
----> 1 pd.Series([0]).to_timestamp()

~/sandbox/pandas/pandas/core/series.py in to_timestamp(self, freq, how, copy)
   4565             new_values = new_values.copy()
   4566
-> 4567         assert isinstance(self.index, (ABCDatetimeIndex, ABCPeriodIndex))
   4568         new_index = self.index.to_timestamp(freq=freq, how=how)
   4569         return self._constructor(new_values, index=new_index).__finalize__(

AssertionError:

Problem description

Users shouldn't see AssertionErrors like this, since they may be disabled when the interpreter starts up.

Expected Output

We probably want this to be a TypeError.

@TomAugspurger TomAugspurger added Bug Error Reporting Incorrect or improved errors from pandas good first issue labels Apr 6, 2020
@TomAugspurger TomAugspurger added this to the 1.1 milestone Apr 6, 2020
@TomAugspurger
Copy link
Contributor Author

Same issue applies to Series.to_period

@dsaxton
Copy link
Member

dsaxton commented Apr 6, 2020

@TomAugspurger Are those assertions in order to placate mypy? Wonder if there's another way of doing that?

@TomAugspurger
Copy link
Contributor Author

Not sure. I would hope that with

if not isinstance(self.index (ABCDatetimeIndex, ABCPeriodIndex)):
    raise TypeError(...)

# hopefully mypy knows the types here

but we can make the assert for mypy after we've done the user-facing check if needed.

@CerseiO
Copy link

CerseiO commented Apr 8, 2020

take

@scriptdruid
Copy link
Contributor

Hello @CerseiO are you working on this or shall I work ?

@CerseiO
Copy link

CerseiO commented May 6, 2020

Hello @vipulrai91, not now, you can work on this.

@scriptdruid
Copy link
Contributor

Hello @vipulrai91, not now, you can work on this.

Thanks for confirming.

@scriptdruid
Copy link
Contributor

take

@scriptdruid
Copy link
Contributor

@TomAugspurger

Does this work ?

>>> pd.Series([0]).to_timestamp()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/vipul/Work/Github/pandas/pandas/core/series.py", line 4665, in to_timestamp
    raise TypeError(f"unsupported Type {self.index}")
TypeError: unsupported Type RangeIndex(start=0, stop=1, step=1)

The code change made was:

if not isinstance(self.index, (ABCDatetimeIndex, ABCPeriodIndex)):
          raise TypeError(f"unsupported Type {self.index}")
new_index = self.index.to_timestamp(freq=freq, how=how)
return self._constructor(new_values, index=new_index).__finalize__(
            self, method="to_timestamp")

@simonjayhawkins
Copy link
Member

simonjayhawkins commented May 8, 2020

  • I have confirmed this bug exists on the latest version of pandas.

the assertion was added in #31126 (i..e not yet released)

on 1.0.3

>>> import pandas as pd
>>>
>>> pd.__version__
'1.0.3'
>>>
>>> pd.Series([0]).to_timestamp()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\pandas\pandas\core\series.py", line 4529, in to_timestamp
    new_index = self.index.to_timestamp(freq=freq, how=how)
AttributeError: 'RangeIndex' object has no attribute 'to_timestamp'
>>>

@jreback jreback added Period Period data type Datetime Datetime data dtype labels May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Error Reporting Incorrect or improved errors from pandas good first issue Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants