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

Coercing DateTimeIndex fails starting with Pandera v0.13.4 #1046

Closed
2 tasks done
davidandreoletti opened this issue Dec 8, 2022 · 5 comments · Fixed by #1057
Closed
2 tasks done

Coercing DateTimeIndex fails starting with Pandera v0.13.4 #1046

davidandreoletti opened this issue Dec 8, 2022 · 5 comments · Fixed by #1057
Labels
bug Something isn't working

Comments

@davidandreoletti
Copy link
Contributor

davidandreoletti commented Dec 8, 2022

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandera.
  • [] (optional) I have confirmed this bug exists on the master branch of pandera.

Describe the bug

When passed the following date time index to DateTime._coerce(...)'s inline function _to_datetime

col = DatetimeIndex(['2021-10-24 00:00:00+00:00', '2021-10-25 00:00:00+00:00',
               '2021-10-26 00:00:00+00:00', '... 00:00:00+00:00', '2021-10-31 00:00:00+00:00'],
              dtype='datetime64[ns, UTC]', name='timestamp', freq=None)

with schema instance built from

class PriceDFSchema(pandera.SchemaModel):
    timestamp: Index[pd.DatetimeTZDtype] = Field(
        coerce=True, dtype_kwargs={"unit": "ns", "tz": "UTC"}
    )
    product_id: Series[pd.Int64Dtype] = Field(coerce=True)
    ...

and starting with #972, Pandera fails to coerce <class 'pandas.core.indexes.datetimes.DatetimeIndex'> data_container into type datetime64[ns, UTC] due to missing dt called here:

and col.dt.tz is None

Pandera/Pandas/Bug Status matrix:

Pandera 0.12.0 / Pandas 1.4.4-1.5.2 => coercion success
Pandera 0.13.4 / Pandas 1.4.4-1.5.2 => coercion failure

The difference seems to be that the coercion happened on a series on #972 while in the code sample above it is on an index.

Workaround

Use Pandera 0.12.0

Code Sample, a copy-pastable example

See above.

Expected behavior

Coercing succeeds

Desktop (please complete the following information):

  • OS: macOS 15.6
  • Version Pandera: 0.13.4
  • Version Pandas: 1.5.2
  • Python 3.9.x

Screenshots

NA

Additional context

NA

@davidandreoletti davidandreoletti added the bug Something isn't working label Dec 8, 2022
@davidandreoletti
Copy link
Contributor Author

davidandreoletti commented Dec 8, 2022

The difference seems to be that the coercion happened on a series on #972 while in the code sample above it is on an index.

@cristianmatache As #972 submitter, and in light with the text above, any idea why Index (vs Series) seems to have no 'dt' field ?

@cristianmatache
Copy link
Contributor

cristianmatache commented Dec 9, 2022

@davidandreoletti it looks like i haven't handled that case properly, only Series' have .dt, indexes don't.

We should add an if statement to check whether hasattr(col, 'dt'), I think something like this should do

            if (
                hasattr(pandas_dtype, "tz")
                and pandas_dtype.tz is not None
            ):
                # localize datetime column so that it's timezone-aware
                if hasattr(col, 'dt') and col.dt.tz is None:  # Series
                    col = col.dt.tz_localize(pandas_dtype.tz, **_tz_localize_kwargs)
                elif col.tz is None:  # Index
                   col = col.tz_localize(pandas_dtype.tz, **_tz_localize_kwargs)
            return col.astype(pandas_dtype)

@cristianmatache
Copy link
Contributor

I can have a look at this early next week, if you need the functionality before that, please feel free to submit a PR.

@davidandreoletti
Copy link
Contributor Author

davidandreoletti commented Jan 5, 2023

I can have a look at this early next week, if you need the functionality before that, please feel free to submit a PR.

Thanks @cristianmatache for the PR #1057 !

@JungeAlexander
Copy link

JungeAlexander commented Feb 19, 2023

Resurfacing this as the issue still seems to persist and PR #1057 remains unmerged. @cristianmatache, could you please sign your commit as explained here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants