Skip to content

Commit

Permalink
Fix dataframe time column period formatting
Browse files Browse the repository at this point in the history
This is a (small) hack that works around changes in how pandas deals
with casting Series to `datetime64` types have units larger than
seconds. We depended on the previous behavior. Not sure if it's
something that will get fixed in pandas, but I made this issue and it's
a breaking change, so hopefully:

pandas-dev/pandas#48574
  • Loading branch information
zaneselvans committed Sep 16, 2022
1 parent 1f3ba5d commit 3e67e5b
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/pudl/metadata/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
from collections.abc import Callable

import numpy as np
import pandas as pd
import pyarrow as pa
import sqlalchemy as sa
Expand Down Expand Up @@ -72,12 +73,12 @@
"""

PERIODS: dict[str, Callable[[pd.Series], pd.Series]] = {
"year": lambda x: x.astype("datetime64[Y]"),
"year": lambda x: pd.Series(np.array(x).astype("datetime64[Y]")),
"quarter": lambda x: x.apply(
pd.tseries.offsets.QuarterBegin(startingMonth=1).rollback
),
"month": lambda x: x.astype("datetime64[M]"),
"date": lambda x: x.astype("datetime64[D]"),
"month": lambda x: pd.Series(np.array(x).astype("datetime64[M]")),
"date": lambda x: pd.Series(np.array(x).astype("datetime64[D]")),
}
"""
Functions converting datetimes to period start times, by time period.
Expand Down

0 comments on commit 3e67e5b

Please sign in to comment.