pandas/tz-localize-without-dst
| Rule | tz-localize-without-dst |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
tz_localize raises on a time that repeats or is skipped at a daylight saving change unless you say what those times become. Pass both ambiguous= and nonexistent=.
local = df["time"].dt.tz_localize("Europe/Copenhagen")local = df["time"].dt.tz_localize("Europe/Copenhagen", ambiguous="infer", nonexistent="shift_forward")What the rule reports
Section titled “What the rule reports”The rule reports df["d"].dt.tz_localize("Europe/Copenhagen") without ambiguous= or without nonexistent=. A zone with daylight saving has an hour each year that happens twice and an hour that never happens. tz_localize raises on a time in either hour, unless ambiguous= and nonexistent= say what to do. On half-hourly data for 2024 in Europe/Copenhagen the call raised.
| Zone | Reported |
|---|---|
a zone with daylight saving, such as "Europe/Copenhagen" |
yes |
"UTC", "GMT", a zone under Etc/, or an offset such as "+01:00" |
no, because they did not raise |
| a zone held in a name | no |
a zone without daylight saving, such as "Asia/Tokyo" |
yes, by mistake |
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
The table lists the values that the two arguments take.
| Argument | Value | Result |
|---|---|---|
ambiguous= |
"infer" |
reads the order of the rows, and needs both copies of the repeated hour |
ambiguous= |
"NaT" |
makes the time missing |
ambiguous= |
a boolean array | says which rows are summer time |
nonexistent= |
"shift_forward", "shift_backward", "NaT" or a Timedelta |
moves the time out of the missing hour, or makes it missing |
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
tz_localize raises for a time that repeats when the clocks go back unless ambiguous= is set, and for a time that is skipped when they go forward unless nonexistent= is set. Pass both to say what those times become.Browse the other rules of the pandas pack.