How to Convert Float to Datetime in Pandas DataFrame? - GeeksforGeeks

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 libraryimport pandas as pd  # Initializing the nested list # with Data setplayer_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 dataframedf = 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 libraryimport pandas as pd  # Initializing the nested list with # Data setplayer_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 dataframedf = 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 libraryimport pandas as pd  # Initializing the nested list with Data setplayer_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 dataframedf = 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 libraryimport pandas as pd  # Initializing the nested list with Data setplayer_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 dataframedf = 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 columnsdf['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.
Python pandas-dataFrame
Python Pandas-exercise
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()