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: to_pydatetime() -> 'pyarrow.lib.DataType' object has no attribute 'unit' #52812

Closed
2 of 3 tasks
s-banach opened this issue Apr 20, 2023 · 6 comments · Fixed by #52952
Closed
2 of 3 tasks

BUG: to_pydatetime() -> 'pyarrow.lib.DataType' object has no attribute 'unit' #52812

s-banach opened this issue Apr 20, 2023 · 6 comments · Fixed by #52952
Labels
Arrow pyarrow functionality Bug Error Reporting Incorrect or improved errors from pandas
Milestone

Comments

@s-banach
Copy link

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 polars as pl
import datetime

pl.Series([datetime.date(2022,12,31)]).to_pandas(use_pyarrow_extension_array=True).dt.to_pydatetime()

"""AttributeError: 'pyarrow.lib.DataType' object has no attribute 'unit'"""

Issue Description

For context, I was feeding a pandas DataFrame with pyarrow dtypes into the dash package, which called s.dt.to_pydatetime() on a date series s. Maybe the problem is dash's fault, but this seems like a pandas bug.

Expected Behavior

Return datetime objects as expected.

Installed Versions

ModuleNotFoundError: No module named 'PySide6'

@s-banach s-banach added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 20, 2023
@mroeschke
Copy link
Member

Can you post the pull traceback?

@mroeschke mroeschke added Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 20, 2023
@s-banach
Copy link
Author

~/.local/lib/python3.9/site-packages/pandas/core/indexes/accessors.py in to_pydatetime(self)
    211 
    212     def to_pydatetime(self):
--> 213         return cast(ArrowExtensionArray, self._parent.array)._dt_to_pydatetime()
    214 
    215     def isocalendar(self):

~/.local/lib/python3.9/site-packages/pandas/core/arrays/arrow/array.py in _dt_to_pydatetime(self)
   2085     def _dt_to_pydatetime(self):
   2086         data = self._data.to_pylist()
-> 2087         if self._dtype.pyarrow_dtype.unit == "ns":
   2088             data = [None if ts is None else ts.to_pydatetime(warn=False) for ts in data]
   2089         return np.array(data, dtype=object)

AttributeError: 'pyarrow.lib.DataType' object has no attribute 'unit'

@mroeschke
Copy link
Member

Okay here's a pandas reproducer

In [1]: import datetime

In [2]: import numpy as np; import pandas as pd; import pyarrow as pa

In [3]: pd.Series(pd.arrays.ArrowExtensionArray(pa.array([datetime.date.today()]))).dt.to_pydatetime()
AttributeError: 'pyarrow.lib.DataType' object has no attribute 'unit'

Not 100% that this should work, datetime.date and datetime.datetime are different objects. Probably should raise a better error message here that the pyarrow type should be pa.timestamp type specifically

@mroeschke mroeschke added Error Reporting Incorrect or improved errors from pandas Arrow pyarrow functionality and removed Needs Info Clarification about behavior needed to assess issue labels Apr 20, 2023
@jbrockmendel
Copy link
Member

isnt this why we have get_unit_from_pa_dtype?

@mroeschke
Copy link
Member

mroeschke commented Apr 21, 2023

get_unit_from_pa_dtype

Looks specifically used for pa.time types, but for a totally different application.

The issue here is date[unit][pyarrow] (and not necessarily timestamp[unit][pyarrow]) type working with a to_pydatetime() method

@phofl phofl added this to the 2.0.1 milestone Apr 22, 2023
@phofl
Copy link
Member

phofl commented Apr 22, 2023

Labelling 2.0.1 for now, think it might make sense back porting a better error message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality Bug Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants