-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
BUG: DataFrame constructor raises error if specify tz dtype dtype='datetime64[ns, UTC]'
#12513
Comments
Trying to look into this. |
Is there any workaround offered for the `dtype='datetime64[ns, UTC]' problem? Any suggestions? |
what are you trying to do? |
this is a reasonable way to deal with this.
|
Thanks. I see the error when using Ibis framework, when I query on a table that has null values in a timestamp with timezone field. I did use something like that as a fix, but it was very slow on queries for large tables. |
@John-Boik that doesn't make sense, you are iterating over the columns. unless you have millions of columns (which would be completely non-performant anyhow) |
The error occurs within Ibis, which calls pandas, which raises an error in ~lib/python3.5/site-packages/pandas/core/internals.py, near line 573: dtype = np.dtype(dtype). |
My crude fix was:
But as I said, it was too slow to work for big tables. |
dtype='datetime64[ns, UTC]'
dtype='datetime64[ns, UTC]'
Pandas also failed to import pandas as pd
df = pd.DataFrame({'a': pd.date_range('2018-01-01', '2018-01-03', tz='Asia/Shanghai')})
da = df['a'].view('int64')
da.view(df['a'].dtype) will generate Traceback (most recent call last)
<ipython-input-62-58aa88ef59a7> in <module>
3 df = pd.DataFrame({'a': pd.date_range('2018-01-01', '2018-01-03', tz='Asia/Shanghai')})
4 da = df['a'].view('int64')
----> 5 da.view(df['a'].dtype)
~/python3.7/site-packages/pandas/core/series.py in view(self, dtype)
632 dtype: int8
633 """
--> 634 return self._constructor(self._values.view(dtype),
635 index=self.index).__finalize__(self)
636
TypeError: data type not understood I have to use the following def view_as(s, dtype):
try:
return s.view(dtype)
except TypeError as e:
if isinstance(dtype, str):
dtype = pd.core.dtypes.dtypes.DatetimeTZDtype.construct_from_string(dtype)
if isinstance(dtype, pd.core.dtypes.dtypes.DatetimeTZDtype):
s = s.view(f'datetime64[{dtype.unit}]')
if dtype.tz:
s = s.dt.tz_localize('utc').dt.tz_convert(dtype.tz)
return s
raise e Actually, the full version of def view_as(s, dtype):
try:
if isinstance(s.dtype, pd.core.dtypes.dtypes.CategoricalDtype):
s = s.cat.codes.values
if isinstance(dtype, pd.core.dtypes.dtypes.CategoricalDtype):
return pd.Categorical.from_codes(s, dtype.categories)
else:
return s.view(dtype)
except TypeError as e:
if isinstance(dtype, str):
dtype = pd.core.dtypes.dtypes.DatetimeTZDtype.construct_from_string(dtype)
if isinstance(dtype, pd.core.dtypes.dtypes.DatetimeTZDtype):
s = s.view(f'datetime64[{dtype.unit}]')
if dtype.tz:
s = s.dt.tz_localize('utc').dt.tz_convert(dtype.tz)
return s
raise e |
While working on googleapis/python-bigquery-pandas#247, I'm able to construct a DataFrame (and series) with
I'm not sure what the pip packages are doing differently than the latest pre-wheel? In the meantime, I'll use timezone naive datetimes in pandas-gbq. |
Update: Passing a timezone as part of the dtype string was officially deprecated in #23990 Construct a DatetimeTZDtype instead. I believe this issue can be closed. |
Is this issue closed? Getting a new error related to how numpy handles
Working Example:
Not Working Example # 1
Not Working Example # 2
|
you would have to try on master and if not u can open an issue |
Code Sample, a copy-pastable example if possible
The error:
Expected Output
output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: