Skip to content

remove np.datetime64 from S1, make adding two TimestampSeries fail #527

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 1 commit into from
Feb 7, 2023

Conversation

Dr-Irv
Copy link
Collaborator

@Dr-Irv Dr-Irv commented Feb 6, 2023

Turns out that it is not possible to have a Series containing np.datetime64 types - they get converted to Timestamp. Discovered this with some experiments that showed we were allowing two Series of Timestamp to be added, which is not possible, so fixed that as well.

@Dr-Irv Dr-Irv requested a review from twoertwein February 6, 2023 16:16
c_series_dt64: pd.Series[np.datetime64] = pd.Series(
[1, 2, 3], dtype="datetime64[ns]"
)
c_series_dt64: TimestampSeries = pd.Series([1, 2, 3], dtype="datetime64[ns]")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to specify dtype=np.datetime64?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. This is consistent with numpy.

>>> import pandas as pd
>>> import numpy as np
>>> pd.Series([1,2,3], dtype=np.datetime64)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\series.py", line 470, in __init__
    data = sanitize_array(data, index, dtype, copy)
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\construction.py", line 622, in sanitize_array
    subarr = _try_cast(data, dtype, copy, raise_cast_failure)
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\construction.py", line 827, in _try_cast
    return maybe_cast_to_datetime(arr, dtype)
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\dtypes\cast.py", line 1321, in maybe_cast_to_datetime
    dtype = _ensure_nanosecond_dtype(dtype)
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\dtypes\cast.py", line 1468, in _ensure_nanosecond_dtype
    raise ValueError(msg)
ValueError: The 'datetime64' dtype has no unit. Please pass in 'datetime64[ns]' instead.

@twoertwein twoertwein merged commit cd78cbb into pandas-dev:main Feb 7, 2023
@twoertwein
Copy link
Member

Thanks @Dr-Irv

@Dr-Irv Dr-Irv deleted the fixtimes branch February 27, 2023 22:18
twoertwein pushed a commit to twoertwein/pandas-stubs that referenced this pull request Apr 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants