[PYTHON] How to set the extended iso8601 format date to the Dataframe index

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

How to set the extended iso8601 format date to the Dataframe index
[Python] How to change the date format (display format)
python / pandas / dataframe / How to get the simplest row / column / index / column
How to set the server time to Japanese time
How to reassign index in pandas dataframe
Correct the week set by index in datetime format
Set the form DateField to type = date in Django
[Blender] How to dynamically set the selection of EnumProperty
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
How to use the generator
How to set the html class attribute in Django's forms.py
How to use the decorator
How to increase the axis
How to start the program
How to set the output resolution for each keyframe in Blender
Determine the date and time format in Python and convert to Unixtime
How to get and set the NTP server name by DHCP
Set the last modified date of the child file to the modified date of the parent directory
Extract the index of the original set list that corresponds to the list of subsets.
How to find the memory address of a Pandas dataframe value
[Python] Explains how to use the format function with an example
How to set up the development environment of ev3dev [Windows version]
How to calculate the autocorrelation coefficient
How to use the zip function
How to read the SNLI dataset
How to get the Python version
[Python] How to import the library
How to overwrite the output to the console
How to calculate date with python
How to use the ConfigParser module
[Python] What is a formal argument? How to set the initial value
How to get a specific column name and index name in pandas DataFrame
How to get the date and time difference in seconds with python
[Python] How to use the enumerate function (extract the index number and element)
[Python] How to set the (client) window size inside the browser with Selenium
[Introduction to Python] How to write a character string with the format function
[Development environment] How to create a data set close to the production DB
How to display the progress bar (tqdm)
[Blender] How to set shape_key with script
How to split and save a DataFrame
How to easily convert format from Markdown
How to set optuna (how to write search space)
How to check the version of Django
How to solve the bin packing problem
Set the time zone to Japan Standard Time
How to set up SVM using Optuna
How to manually update the AMP cache
[Linux] How to use the echo command
How to use the Linux grep command
How to set xg boost using Optuna
[Pandas] Expand the character string to DataFrame
Convert Python date types to RFC822 format
How to access the Datastore from the outside
How to convert DateTimeField format in Django
Change the Key of Object on S3 from normal date format to Hive format
The first step to log analysis (how to format and put log data in Pandas)
[Introduction to Python] How to get the index of data with a for statement