Skip to content

BUG: Calling tz_localize suppresses ValueError when converting from NaT to np.int64 #39162

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
2 of 3 tasks
vdonato opened this issue Jan 14, 2021 · 4 comments
Closed
2 of 3 tasks
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions

Comments

@vdonato
Copy link

vdonato commented Jan 14, 2021

  • 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

import numpy as np
import pandas as pd

ser = pd.Series([pd.NaT], dtype="datetime64[ns]")
ser = ser.dt.tz_localize("America/Los_Angeles")

print(ser.astype(np.int64)[0])

Problem description

The code snippet above prints -9223372036854775808 (-263) instead of raising a ValueError, which is the behavior that would occur if tz_localize were not called.

Expected Output

Should raise a ValueError: Cannot convert NaT values to integer.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.0-36-generic
Version : #40~20.04.1-Ubuntu SMP Wed Jan 6 10:15:55 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.0
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@vdonato vdonato added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 14, 2021
@vdonato vdonato changed the title BUG: Calling tz_localize suppresses ValueError when converting from NaT -> np.int64 BUG: Calling tz_localize suppresses ValueError when converting from NaT to np.int64 Jan 14, 2021
@jreback
Copy link
Contributor

jreback commented Jan 14, 2021

hmm the error is not that a datetime64[ns, UTC} is raise, rather its the reverse, we are raising on a all-NaT datetime64[ns] when converting to np.int64 which is unlike numpy

In [51]: ser.dt.tz_localize(None).values.astype('int64')
Out[51]: array([-9223372036854775808])

while we raise

note that there is a warning on this in current master for using .astype though .view has the same behavior

@jreback
Copy link
Contributor

jreback commented Jan 14, 2021

cc @jbrockmendel

@jreback jreback added Dtype Conversions Unexpected or buggy dtype conversions Datetime Datetime data dtype labels Jan 14, 2021
@jreback jreback added this to the Contributions Welcome milestone Jan 14, 2021
@lithomas1 lithomas1 removed the Needs Triage Issue that has not been reviewed by a pandas team member label Feb 4, 2021
@jbrockmendel
Copy link
Member

ATM both of these cases issue a FutureWarning that the astype is deprecated and will raise. i.e. i think this is as correct as its going to get until 2.0

@jbrockmendel jbrockmendel added the Closing Candidate May be closeable, needs more eyeballs label Mar 31, 2021
@mroeschke
Copy link
Member

Agreed. Since this behavior was deprecated, closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

No branches or pull requests

5 participants