I had some trouble so I will write it down.
dt.ipynb
import pandas as pd
#JST sample data
data = [
{"count":224, "time":"2016-01-01T09:00:00+0900"},
{"count":198, "time":"2016-01-01T12:00:00+0900"},
{"count":312, "time":"2016-01-01T20:00:00+0900"},
]
df = pd.DataFrame(data)
df
Of course it's still a string.
count | time | |
---|---|---|
0 | 224 | 2016-01-01T09:00:00+0900 |
1 | 198 | 2016-01-01T12:00:00+0900 |
2 | 312 | 2016-01-01T20:00:00+0900 |
dt.ipynb
#iso8601 to_datetime()If you pass it to, the UTC time will be returned, so convert it to JST.
dt = pd.to_datetime(df["time"].tolist()).tz_localize("UTC").tz_convert("Asia/Tokyo")
df.set_index(dt, inplace=True)
df.drop(["time"], axis=1, inplace=True)
df
count | |
---|---|
2016-01-01 09:00:00+09:00 | 224 |
2016-01-01 12:00:00+09:00 | 198 |
2016-01-01 20:00:00+09:00 | 312 |
dt.ipynb
type(df.index)
pandas.tseries.index.DatetimeIndex
I want to know if there is a smarter way ...
Recommended Posts