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.
Similar Reads
How to Convert Datetime to Date in Pandas ?
DateTime is a collection of dates and times in the format of "yyyy-mm-dd HH:MM:SS" where yyyy-mm-dd is referred to as the date and HH:MM:SS is referred to as Time. In this article, we are going to discuss converting DateTime to date in pandas. For that, we will extract the only date from DateTime us
4 min read
How to Convert Integer to Datetime in Pandas DataFrame?
Let's discuss how to convert an Integer to Datetime in it. Now to convert Integers to Datetime in Pandas DataFrame. Syntax of  pd.to_datetimedf['DataFrame Column'] = pd.to_datetime(df['DataFrame Column'], format=specify your format)Create the DataFrame to Convert Integer to Datetime in Pandas Check
2 min read
How to Convert String to Float in Pandas DataFrame
Converting Strings to Float in Pandas DataFrame is a very crucial step for data analysis. Converting string to float values can help you perform various arithmetic operations and plot graphs. In this article, we'll look at different ways to convert a string to a float in DataFrame. Creating Sample D
4 min read
How to Convert Floats to Strings in Pandas DataFrame?
In this post, we'll see different ways to Convert Floats to Strings in Pandas Dataframe? Pandas Dataframe provides the freedom to change the data type of column values. We can change them from Integers to Float type, Integer to String, String to Integer, Float to String, etc. There are three methods
4 min read
How to Convert Integers to Floats in Pandas DataFrame?
Pandas Dataframe provides the freedom to change the data type of column values. We can change them from Integers to Float type, Integer to String, String to Integer, etc. There are 2 methods to convert Integers to Floats: Method 1: Using DataFrame.astype() method Syntax :Â DataFrame.astype(dtype, co
4 min read
How to convert datetime to date in Python
In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f
3 min read
Convert column type from string to datetime format in Pandas dataframe
To perform time-series operations, dates should be in the correct format. Let's learn how to convert a Pandas DataFrame column of strings to datetime format. Pandas Convert Column To DateTime using pd.to_datetime()pd.to_datetime() function in Pandas is the most effective way to handle this conversio
4 min read
How to Convert String to Date or Datetime in Polars
When working with data, particularly in CSV files or databases, it's common to find dates stored as strings. If we're using Polars, a fast and efficient DataFrame library written in Rust (with Python bindings), we'll often need to convert these strings into actual date or datetime objects for easier
5 min read
Convert Floats to Integers in a Pandas DataFrame
Let us see how to convert float to integer in a Pandas DataFrame. We will be using the astype() method to do this. It can also be done using the apply() method. Convert Floats to Integers in a Pandas DataFrameBelow are the ways by which we can convert floats to integers in a Pandas DataFrame: Using
3 min read
How to change the Pandas datetime format in Python?
Prerequisites: Pandas The date-time default format is "YYYY-MM-DD". Hence, December 8, 2020, in the date format will be presented as "2020-12-08". The datetime format can be changed and by changing we mean changing the sequence and style of the format. Function used strftime() can change the date
1 min read