Skip to content
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: Conversion of datetime64[ns, UTC] to Arrow C format string is not implemented #54239

Closed
2 of 3 tasks
jonmmease opened this issue Jul 24, 2023 · 2 comments · Fixed by #54246
Closed
2 of 3 tasks

BUG: Conversion of datetime64[ns, UTC] to Arrow C format string is not implemented #54239

jonmmease opened this issue Jul 24, 2023 · 2 comments · Fixed by #54246
Labels
Bug Interchange Dataframe Interchange Protocol

Comments

@jonmmease
Copy link
Contributor

jonmmease commented Jul 24, 2023

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

data = pd.DataFrame(
    {
        "ts": pd.date_range("2018-01-01", periods=5, freq="D"),
        "ts_utc": pd.date_range("2018-01-01", periods=5, freq="D").tz_localize("UTC"),
    }
)

dfi = data.__dataframe__()

print(dfi.get_column_by_name("ts").dtype)
print(dfi.get_column_by_name("ts_utc").dtype)
(<DtypeKind.DATETIME: 22>, 64, 'tsn:', '=')

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In[25], line 13
     10 dfi = data.__dataframe__()
     12 print(dfi.get_column_by_name("ts").dtype)
---> 13 print(dfi.get_column_by_name("ts_utc").dtype)

File ~/miniconda3/envs/dev/lib/python3.10/site-packages/pandas/_libs/properties.pyx:36, in pandas._libs.properties.CachedProperty.__get__()

File ~/miniconda3/envs/dev/lib/python3.10/site-packages/pandas/core/interchange/column.py:126, in PandasColumn.dtype(self)
    124     raise NotImplementedError("Non-string object dtypes are not supported yet")
    125 else:
--> 126     return self._dtype_from_pandasdtype(dtype)

File ~/miniconda3/envs/dev/lib/python3.10/site-packages/pandas/core/interchange/column.py:141, in PandasColumn._dtype_from_pandasdtype(self, dtype)
    137 if kind is None:
    138     # Not a NumPy dtype. Check if it's a categorical maybe
    139     raise ValueError(f"Data type {dtype} not supported by interchange protocol")
--> 141 return kind, dtype.itemsize * 8, dtype_to_arrow_c_fmt(dtype), dtype.byteorder

File ~/miniconda3/envs/dev/lib/python3.10/site-packages/pandas/core/interchange/utils.py:90, in dtype_to_arrow_c_fmt(dtype)
     87     resolution = re.findall(r"\[(.*)\]", typing.cast(np.dtype, dtype).str)[0][:1]
     88     return ArrowCTypes.TIMESTAMP.format(resolution=resolution, tz="")
---> 90 raise NotImplementedError(
     91     f"Conversion of {dtype} to Arrow C format string is not implemented."
     92 )

NotImplementedError: Conversion of datetime64[ns, UTC] to Arrow C format string is not implemented.

Issue Description

We're updating the Altair visualization library to extract DataFrame schema information through the DataFrame Interchange Protocol. With pandas 2.0.3, This process currently fails for timestamps that include a timezone (but works for timestamps without a timezone).

Expected Behavior

I expect it to be possible to extract data type information through the DataFrame Interchange Protocol for timestamp-with-timezone columns.

Installed Versions

INSTALLED VERSIONS

commit : 0f43794
python : 3.10.11.final.0
python-bits : 64
OS : Darwin
OS-release : 22.5.0
Version : Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8

pandas : 2.0.3
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : None
pytest : 7.3.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.13.2
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2023.5.0
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 12.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : 2.0.19
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@jonmmease jonmmease added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 24, 2023
@mroeschke mroeschke added Interchange Dataframe Interchange Protocol and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 24, 2023
@jonmmease
Copy link
Contributor Author

Thanks for the quick fix @mroeschke!

@zero-stroke
Copy link

zero-stroke commented Jan 19, 2024

I get this error when converting pandas df to polars. Fix:

df['timestamp'] = df['timestamp'].dt.tz_convert(None)
pl_df = pl.from_dataframe(df)

pl_df = pl_df.with_columns(
    pl.col('timestamp').cast(pl.Datetime).dt.replace_time_zone("UTC").alias('timestamp')
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Interchange Dataframe Interchange Protocol
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants