DataFrame.to_datetime()

Pandas date & time Pandas

Cast dtype to a datetime with options.
Let us change Date column to datetime.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex'],
         'dt_start':['1/1/2020','2/1/2020','5/1/2020']
         }
my_data = pd.DataFrame(data=my_dict)
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'])
print(my_data)
Output
   NAME   dt_start
0  Ravi 2020-01-01
1  Raju 2020-02-01
2  Alex 2020-05-01
The column dt_start is changed to datetime type.

Present Date and time

print(pd.to_datetime('now'))
Output
2020-04-11 02:04:21.773912

Let us check how we can change other formats.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex'],
         'dt_start':['1-1-2020','2-1-2020','5-1-2020']
         }
my_data = pd.DataFrame(data=my_dict)
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'])
print(my_data)
Output

   NAME   dt_start
0  Ravi 2020-01-01
1  Raju 2020-02-01
2  Alex 2020-05-01
In above code dt_start is now datetime dtype.

Let us add three more columns and store year , month and day in these columns. We will also change the date format.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex'],
         'dt_start':['1-Jan-2020','2-Feb-2020','5-Mar-2020']
         }
my_data = pd.DataFrame(data=my_dict)
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'],dayfirst=True)

my_data['year']=my_data['dt_start'].dt.year
my_data['month']=my_data['dt_start'].dt.month
my_data['day']=my_data['dt_start'].dt.day
print(my_data)
Output
   NAME   dt_start  year  month  day
0  Ravi 2020-01-01  2020      1    1
1  Raju 2020-02-02  2020      2    2
2  Alex 2020-03-05  2020      3    5

dayfirst

Default value is False, We can set dayfirst=True.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex'],
         'dt_start':['1/1/2020','2/1/2020','5/1/2020']
}
my_data = pd.DataFrame(data=my_dict)
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'],dayfirst=True)
my_data['year']=my_data['dt_start'].dt.year
my_data['month']=my_data['dt_start'].dt.month
my_data['day']=my_data['dt_start'].dt.day
print(my_data)
Output , check that the first number of the date is taken as day here.
   NAME   dt_start  year  month  day
0  Ravi 2020-01-01  2020      1    1
1  Raju 2020-01-02  2020      1    2
2  Alex 2020-01-05  2020      1    5

yearfirst

Default value is False, we can set yearfirst=True
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex'],
         'dt_start':['2020-Jan-2','2021-Feb-5','2019-Mar-12']
         }
my_data = pd.DataFrame(data=my_dict)
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'],yearfirst=True)

my_data['year']=my_data['dt_start'].dt.year
my_data['month']=my_data['dt_start'].dt.month
my_data['day']=my_data['dt_start'].dt.day
print(my_data)
Output
   NAME   dt_start  year  month  day
0  Ravi 2020-01-02  2020      1    2
1  Raju 2021-02-05  2021      2    5
2  Alex 2019-03-12  2019      3   12

format

We can specify the format like this format='%Y%m%d'
import pandas as pd 
my_dict={'dt_start':['22000105']}
my_data = pd.DataFrame(data=my_dict)
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'],format='%Y%m%d')
print(my_data)
Output
    dt_start
0 2200-01-05
You can get a list of directives to create formatted output by using strftime() . Check the example at the end of this page on how the format is used to create datetime by including Hour, minute and seconds.

errors

We can set errors as raise, coerce or ignore.
import pandas as pd 
my_dict={'dt_start':['11000101']}
my_data = pd.DataFrame(data=my_dict)
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'],format='%Y%m%d',errors='coerce')
print(my_data)
Output is here
  dt_start
0      NaT
Let us change errors='ignore'
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'],format='%Y%m%d',errors='ignore')
Output
              dt_start
0  1100-01-01 00:00:00
Let us change errors='raise'
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'],format='%Y%m%d',errors='raise')
This will generate error OutOfBoundsDatetime

When to use ignore and coerce.

If our column has blank data ( specially when reading from Excel file ) and we want to continue with rest of the data then better to use coerce. By using ignore we may not able to convert to datatime format for all the rows.

As you have seen above how we have created datetime by using current date and time. Now let us find out Year, Month, day , week day, day name , hour , minutes and seconds by using current date and time.
import pandas as pd 
present_time=pd.to_datetime('now')
print('HOUR: ',present_time.hour)
print('Minutes:', present_time.minute)
print('Seconds :',present_time.second)
print('Year :',present_time.year)
print('Month :',present_time.month)
print('Day :',present_time.day)
print('dayofweek :',present_time.dayofweek)
print('day_name :',present_time.day_name())
Output
HOUR:  10
Minutes: 12
Seconds : 57
Year : 2020
Month : 4
Day : 13
dayofweek : 0
day_name : Monday
We can use any date and time by using strftime() formats and generate parts of the datetime object.
import pandas as pd 
present_time=pd.to_datetime('12252020:233445',format='%m%d%Y:%H%M%S')
print('HOUR: ',present_time.hour)
print('Minutes:', present_time.minute)
print('Seconds :',present_time.second)
print('Year :',present_time.year)
print('Month :',present_time.month)
print('Day :',present_time.day)
print('dayofweek :',present_time.dayofweek)
print('day_name :',present_time.day_name())
Output
HOUR:  23
Minutes: 34
Seconds : 45
Year : 2020
Month : 12
Day : 25
dayofweek : 4
day_name : Friday
Pandas date& time date_range() period_range() strftime()


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer