简体   繁体   中英

How to use localized times for plotting? The datetime series is localized to my timezone but plotting is still done with the original times (Pandas)

Dataframe df has a column " start " with a dtype of datetime64[ns]:

df.head()

    start                   energy
0   2017-09-06 09:38:24     4787.329
1   2017-09-06 14:30:02     3448.111
2   2017-09-07 08:49:46     6748.579
3   2017-09-07 07:14:35     4216.576
4   2017-09-07 13:21:49     5695.689

Next, the column is localized:

df["start"] = df["start"].dt.tz_localize(tz = "EET")

    start                       energy
0   2017-09-06 09:38:24+03:00   4787.329
1   2017-09-06 14:30:02+03:00   3448.111
2   2017-09-07 08:49:46+03:00   6748.579
3   2017-09-07 07:14:35+03:00   4216.576
4   2017-09-07 13:21:49+03:00   5695.689

Desired outcome: I want to use the localized times for plotting (for example 12:38:24 in the first line, instead of the original 09:38:24).

Problem: Any plotting that I do uses the original times (for example 09:38:24 in the first line).

How do I plot (and do any other further analysis) using only the localized times (the times in my timezone, EET)?

I would just add timedelta(hours = 3) to the series, but it won't help, because due to daylight savings, the difference is only 2 hours part of the year).

I also don't want to make the "start" column into index.

I found the answer: First, the column must be localized to UTC. Then, it has to be converted into EET.

So all in one line:

df["start"] = df["start"].dt.tz_localize("UTC").dt.tz_convert(tz = "EET")

Result:

    start                       energy
0   2017-09-06 12:38:24+03:00   4787.329
1   2017-09-06 17:30:02+03:00   3448.111
2   2017-09-07 11:49:46+03:00   6748.579
3   2017-09-07 10:14:35+03:00   4216.576
4   2017-09-07 16:21:49+03:00   5695.689

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM