How to Convert String to Datetime Format in Pandas Python

Convert DataFrame column type from string to datetime

The easiest way is to use to_datetime:

df['col'] = pd.to_datetime(df['col'])

It also offers a dayfirst argument for European times (but beware this isn't strict).

Here it is in action:

In [11]: pd.to_datetime(pd.Series(['05/23/2005']))
Out[11]:
0 2005-05-23 00:00:00
dtype: datetime64[ns]

You can pass a specific format:

In [12]: pd.to_datetime(pd.Series(['05/23/2005']), format="%m/%d/%Y")
Out[12]:
0 2005-05-23
dtype: datetime64[ns]

How to convert string to datetime format in pandas python?

Use to_datetime. There is no need for a format string since the parser is able to handle it:

In [51]:
pd.to_datetime(df['I_DATE'])

Out[51]:
0 2012-03-28 14:15:00
1 2012-03-28 14:17:28
2 2012-03-28 14:50:50
Name: I_DATE, dtype: datetime64[ns]

To access the date/day/time component use the dt accessor:

In [54]:
df['I_DATE'].dt.date

Out[54]:
0 2012-03-28
1 2012-03-28
2 2012-03-28
dtype: object

In [56]:
df['I_DATE'].dt.time

Out[56]:
0 14:15:00
1 14:17:28
2 14:50:50
dtype: object

You can use strings to filter as an example:

In [59]:
df = pd.DataFrame({'date':pd.date_range(start = dt.datetime(2015,1,1), end = dt.datetime.now())})
df[(df['date'] > '2015-02-04') & (df['date'] < '2015-02-10')]

Out[59]:
date
35 2015-02-05
36 2015-02-06
37 2015-02-07
38 2015-02-08
39 2015-02-09

Convert string to datetime pandas

Is necessary specify format of string with this reference.
There is no year, so output year is default:

df['date'] = pd.to_datetime(df['date'], format='%b %d %H:%M:%S.%f')
print (df)
date
0 1900-03-16 03:40:24.411
1 1900-03-16 03:40:25.415
2 1900-03-16 03:40:28.532
3 1900-03-16 03:40:30.539
4 1900-03-14 03:20:30.337
5 1900-03-14 03:20:31.340
6 1900-03-14 03:20:37.415

You can add some year to column and then parse it like:

df['date'] = pd.to_datetime('2020 ' + df['date'], format='%Y %b %d %H:%M:%S.%f')
print (df)
date
0 2020-03-16 03:40:24.411
1 2020-03-16 03:40:25.415
2 2020-03-16 03:40:28.532
3 2020-03-16 03:40:30.539
4 2020-03-14 03:20:30.337
5 2020-03-14 03:20:31.340
6 2020-03-14 03:20:37.415

Convert string to datetime - python dataframe

You just need to specify the format parameter to '%d/%m/%Y' to explicitly tell the date format as commented. Or set dayfirst to True. A datetime object actually has information for year, month, day, and time, so to get just month and year displayed, you'll have to convert back to string:

df['Date'] = pd.to_datetime(df['Date'], dayfirst=True).dt.strftime('%Y-%m')

Converting/Modifying non-standard string into datetime for Python DataFrame

To convert it to a datetime, use pd.to_datetime():

df['date'] = pd.to_datetime(df['date'], format="%m/%d/%y %H:%M")

To get the month out of it, use this after you've run the above:

df['month'] = df['date'].dt.month

To convert it into a YYYY-MM format:

df['year-month'] = df['a'].dt.strftime('%Y-%m')

As we discussed in chat, this wasn't working because the column names were copied as rows into the dataframe at various points.

Converting String to datetime (from Week number/Year to dd/mm/yy) in a pandas DataFrame

You can use pd.to_datetime:

df['Time Converted'] = pd.to_datetime('1/' + df['Time'], format='%w/%U/%Y')
print(df)

# Output
Time Time Converted
0 01/2021 2021-01-04
1 02/2021 2021-01-11
2 04/2021 2021-01-25
3 05/2021 2021-02-01
  • %w: Weekday number
  • %U: Week number of the year
  • %Y: Year

Update

I would like to get the last day of the week and not Monday. (e.g. 10/01/2021 instead of 04/01/2021).

df['Time Converted'] = pd.to_datetime('0/' + df['Time'], format='%w/%U/%Y') \
+ pd.DateOffset(days=7)
print(df)

# Output
Time Time Converted
0 01/2021 2021-01-10
1 02/2021 2021-01-17
2 04/2021 2021-01-31
3 05/2021 2021-02-07

Note: adjust the day of the week (0, 1 or 6) according to your locale.

Get datetime format from string python

In pandas, this is achieved by pandas._libs.tslibs.parsing.guess_datetime_format

from pandas._libs.tslibs.parsing import guess_datetime_format

guess_datetime_format('2021-01-01')

# '%Y-%m-%d'

As there will always be an ambiguity on the day/month, you can specify the dayfirst case:

guess_datetime_format('2021-01-01', dayfirst=True)
# '%Y-%d-%m'


Related Topics



Leave a reply



Submit