Skip to content

Commit

Permalink
Dropping the dayfirst argument for xlsx, xls and xlsm. (#143)
Browse files Browse the repository at this point in the history
* drop the dayfirst argument

* black
  • Loading branch information
Ahmad-Wahid authored Aug 29, 2023
1 parent 07227f6 commit 3675469
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions timely_beliefs/beliefs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,11 @@ def read_csv(
if col not in kwargs.get("usecols", [])
]
ext = find_out_extension(path)
dayfirst = None
if ext.lower() == "csv":
df = pd.read_csv(path, **kwargs)
elif ext.lower() in ("xlsm", "xlsx", "xls"):
dayfirst = kwargs.pop("dayfirst", None)
df = pd.read_excel(path, **kwargs) # requires openpyxl
else:
raise TypeError(
Expand All @@ -642,7 +644,7 @@ def read_csv(
df = df[[col for col in kwargs["usecols"] if col in df.columns]]

# Special cases for simple time series
df = interpret_special_read_cases(df, sensor, resample, timezone)
df = interpret_special_read_cases(df, sensor, resample, timezone, dayfirst)

# Apply optionally set belief timing
if belief_horizon is not None and belief_time is not None:
Expand Down Expand Up @@ -723,7 +725,11 @@ def fill_in_sources(


def interpret_special_read_cases(
df: pd.DataFrame, sensor: "classes.Sensor", resample: bool, timezone: Optional[str]
df: pd.DataFrame,
sensor: "classes.Sensor",
resample: bool,
timezone: Optional[str],
dayfirst: bool,
) -> pd.DataFrame:
"""Interpret the read-in data, either as event starts and event values (2 cols),
or as event starts, belief times and event values (3 cols).
Expand All @@ -734,6 +740,11 @@ def interpret_special_read_cases(
if len(df.columns) == 2:
# datetime in 1st column and value in 2nd column
df.columns = ["event_start", "event_value"]
if dayfirst:
df["event_start"] = pd.to_datetime(
df["event_start"], dayfirst=dayfirst
).dt.to_pydatetime()

df["event_start"] = convert_to_timezone(
df["event_start"],
timezone_to_convert_to=sensor.timezone,
Expand All @@ -758,6 +769,13 @@ def interpret_special_read_cases(
elif len(df.columns) == 3:
# datetimes in 1st and 2nd column, and value in 3rd column
df.columns = ["event_start", "belief_time", "event_value"]
if dayfirst:
df["event_start"] = pd.to_datetime(
df["event_start"], dayfirst=dayfirst
).dt.to_pydatetime()
df["belief_time"] = pd.to_datetime(
df["belief_time"], dayfirst=dayfirst
).dt.to_pydatetime()
df["event_start"] = convert_to_timezone(
df["event_start"],
timezone_to_convert_to=sensor.timezone,
Expand Down

0 comments on commit 3675469

Please sign in to comment.