Last active
January 8, 2025 13:16
-
-
Save jmquintana79/02be1acae35540a99b20ef3ca825ba64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# validate dt column exits | |
assert "dt_local" in df.columns.tolist() | |
# make sure is a dt column | |
df["dt_local"] = pd.to_datetime(df["dt_local"]) | |
# set to local time (Europe/Madrid) | |
s_local_time = "Europe/Madrid" | |
df["local"] = df["dt_local"].dt.tz_localize(s_local_time, ambiguous=True) | |
# conversion local to UTC | |
df["dt_utc"] = df["local"].dt.tz_convert("UTC") | |
# to dt | |
df["dt_utc"] = pd.to_datetime(df["dt_utc"]) | |
df["dt_utc"] = df["dt_utc"].dt.tz_localize(None) | |
# remove local | |
df.drop("local", axis = 1, inplace=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment