Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Datetime Conversion from a particular ISO format

Many thanks for your help in advance.

I'm trying to convert a datetime which is a string in ISO Format to a datetime object. But have tried many methods without being successful. Please your help with this.

As an example, I have a dataframe which the column time is similar as shown below. This was extracted from a database and this was the output's format.

2018-12-04T04:39:26Z
2018-12-04T05:10:54.6Z
2018-12-04T05:17:32Z
2018-12-04T10:51:20.5Z
...

What I have tried so far (many attempts) but not being successful:

df.index = pd.to_datetime(df.index, format = "%Y-%m-%dT%H:%M:%SZ", errors='ignore')

df.index = pd.to_datetime(df.index)

df.time = df.time.map(lambda x: pd.to_datetime(dt.datetime.strptime(x, '%Y-%m-%dT%H:%M:%SZ'), format = '%d/%m/%Y %H:%M'))

Thanks Again!

like image 971
J. Reyes Avatar asked Dec 06 '18 18:12

J. Reyes


People also ask

How do I change pandas datetime format?

Pandas Convert Date to String Format – To change/convert the pandas datetime ( datetime64[ns] ) from default format to String/Object or custom format use pandas. Series. dt. strftime() method.

How do I change the date format from YYYY MM DD in pandas?

Use pandas. to_datetime() to change String to “yyyymmdd” Format. If You have a date in "yymmdd" format in the DataFrame column, and to change it from a string to a date ('yyyy-mm-dd') format. Yields below output.

What is the method in pandas to change values into date time data type?

to_datetime. Convert argument to datetime. This function converts a scalar, array-like, Series or DataFrame /dict-like to a pandas datetime object.


2 Answers

I wanted to answer this before. In the end I just made a function that deals with different data inputs and also creates a dataframe with column names. Thanks ALollz for your comment regarding pd.to_datetime(df.index, errors='coerce').

So to convert the index from a string in an ISO format I established and followed this sequence:

df = pd.DataFrame([[-1.8, '2018-09-14T13:36:00Z']], columns = ['current', 'time'])
df.set_index('time', inplace = True)   # make it your index by using the inplace=True
df.index = pd.to_datetime(df.index, errors='coerce')

After the conversion to datetime, check that dates are correct. You may need to specify the format if they are wrong read.

Thanks!

like image 72
J. Reyes Avatar answered Sep 24 '22 20:09

J. Reyes


A bit late to the party, but I believe that this response needs to be visible to ease people's lives.

If, as you said, it was extracted from a database, then you can do it directly at the moment of establishing your dataframe. Most pandas read functions have a parameter parse_dates. As said in the documentation:

Note: A fast-path exists for iso8601-formatted dates.

So even if you have 2 or more columns with dates, you can do it in an extremely easy manner.

df = pd.read_csv("x.csv", parse_dates=['Date1', "Date2"], names=["ID", "Date1", "Date2"])
like image 24
Rysicin Avatar answered Sep 22 '22 20:09

Rysicin