Skip to content
Related Articles

Related Articles

How to Convert Float to Datetime in Pandas DataFrame?
  • Last Updated : 29 Aug, 2020

Pandas Dataframe provides the freedom to change the data type of column values. We can change them from Integers to Float type, Integer to Datetime, String to Integer, Float to Datetime, etc. For converting float to DateTime we use pandas.to_datetime() function and following syntax is used :

Syntax: pandas.to_datetime(arg, errors=’raise’, dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin=’unix’, cache=False)

Example 1: Converting one column from float to ‘yyyymmdd’ format using pandas.to_datetime()

Python3




# importing pandas library
import pandas as pd
  
# Initializing the nested list 
# with Data set
player_list = [[20200112.0,'Mathematics'], 
               [20200114.0,'English'],
               [20200116.0,'Physics'], 
               [20200119.0,'Chemistry'],
               [20200121.0,'French'], 
               [20200124.0,'Biology'], 
               [20200129.0,'Sanskrit']]
  
# creating a pandas dataframe
df = pd.DataFrame(player_list,columns=['Dates','Test'])
  
# printing dataframe 
print(df)
print()
  
# checking the type 
print(df.dtypes)

Output:



After changing the datatype.

Python3




# converting the float to datetime format 
df['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d'
  
# printing dataframe 
print(df)
print()
  
print(df.dtypes)

Output:

In the above example, we change the data type of column ‘Dates‘ from ‘float64‘ to ‘datetime64[ns]‘ type.

Example 2: If the data frame column is in yymmdd format and we have to convert it to yyyymmdd format

Python3






# importing pandas library
import pandas as pd
  
# Initializing the nested list with 
# Data set
player_list = [[180112.0,'Mathematics'],
               [180114.0,'English'],
               [180116.0,'Physics'],
               [180119.0,'Chemistry'],
               [180121.0,'French'],
               [180124.0,'Biology'],
               [180129.0,'Sanskrit']]
  
# creating a pandas dataframe
df = pd.DataFrame(player_list,columns=['Dates','Test'])
  
# printing dataframe 
print(df)
print()
  
# checking the type 
print(df.dtypes)

Output:

After changing the datatype.

Python3




# converting the float to datetime format 
df['Dates'] = pd.to_datetime(df['Dates'], format='%y%m%d'
  
# printing dataframe 
print(df)
print()
  
print(df.dtypes)

Output:

In the above example, we change the data type of column ‘Dates‘ from ‘float64‘ to ‘datetime64[ns]‘ and format from ‘yymmdd‘ to ‘yyyymmdd‘.

Example 3: When we have to convert the float column to Date and Time format

Python3




# importing pandas library
import pandas as pd
  
# Initializing the nested list with Data set
player_list = [[20200112082520.0,'Mathematics'],
               [20200114085020.0,'English'],
               [20200116093529.0,'Physics'],
               [20200119101530.0,'Chemistry'],
               [20200121104060.0,'French'],
               [20200124113541.0,'Biology'],
               [20200129125023.0,'Sanskrit']]
  
# creating a pandas dataframe
df = pd.DataFrame(player_list,columns=['Dates','Test'])
  
# printing dataframe 
print(df)
print()
  
# checking the type 
print(df.dtypes)

Output:

After changing the datatype.

Python3




# converting the float to datetime format 
df['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d%H%M%S'
  
# printing dataframe 
print(df)
print()
  
print(df.dtypes)

Output:

In the above example, we change the data type of column ‘Dates‘ from ‘float64‘ to ‘datetime64[ns]‘ and format to Date and Time

Example 4: Converting multiple columns from float to ‘yyyymmdd‘ format using pandas.to_datetime()

Python3




# importing pandas library
import pandas as pd
  
# Initializing the nested list with Data set
player_list = [[20200112.0,'Mathematics',20200113.0],
               [20200114.0,'English',20200115.0],
               [20200116.0,'Physics',20200117.0],
               [20200119.0,'Chemistry',20200120.0],
               [20200121.0,'French',20200122.0],
               [20200124.0,'Biology',20200125.0],
               [20200129.0,'Sanskrit',20200130.0]]
  
# creating a pandas dataframe
df = pd.DataFrame(player_list,columns=['Starting_Date','Test','Ending_Date'])
  
# printing dataframe 
print(df)
print()
  
# checking the type 
print(df.dtypes)

Output:

After changing the datatype.

Python3




# converting the float to datetime format 
# in multiple columns
df['Starting_Date'] = pd.to_datetime(df['Starting_Date'], 
                                     format='%Y%m%d'
df['Ending_Date'] = pd.to_datetime(df['Ending_Date'],
                                   format='%Y%m%d'
  
# printing dataframe 
print(df)
print()
  
print(df.dtypes)

Output:

In the above example, we change the data type of columns ‘Starting_Date‘ and ‘Ending_Date‘ from ‘float64‘ to ‘datetime64[ns]‘ type.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :