In this post, we will learn How to Convert Float to datetime in Pandas Dataframe or convert float64 to datetime in Pandas. The Pandas is a data analytical library that store data in tabular form, and the table in Pandas is called a dataframe that contains rows and column. It is great to help explore, clean and process data. It supports the integration of different file formats “csv, excel, SQL, JSON, parquet,”.To use a Pandas library first we have to install it on the local system by using the pip command “pip install pandas” and import it into our code by using “import pandas as pd” to use its functions.
1. How to convert float to datetime in Pandas Dataframe
In this example, The dataframe column ‘Admission_date’ contains the value of date as type float64 of format yyyymmdd. To convert float date value to datetime the Pandas dataframe pd.to_datetime() function is used that takes the first parameter is the column name, and the Second parameter is the format of date in which, we need to convert. We have passed date format ‘%m%d%y’ which is equal to Python pandas float to datetime in format YYYYMMDD.
import pandas as pd
import numpy as np
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Subject': ['Math', 'Math', 'Math', 'Phy'],
'Admission_date':[2022615.0,2022617.0,2022616.0,2022618.0]
}
df = pd.DataFrame(data)
print('Existing datatype:\n',df.dtypes)
df['Admission_date'] = pd.to_datetime(df['Admission_date'],format='%Y%m%d')
print('\n',df)
print('datatype after conversion:',df.dtypes)
Output
Existing datatype:
Name object
Marks int64
Subject object
Admission_date float64
dtype: object
Name Marks Subject Admission_date
0 Jack 97 Math 2022-06-15
1 Rack 97 Math 2022-06-17
2 Max 100 Math 2022-06-16
3 David 100 Phy 2022-06-18
datatype after conversion: Name object
Marks int64
Subject object
Admission_date datetime64[ns]
dtype: object
- Pandas convert multiple columns to float
- Convert string column to float in Pandas
- Convert string column to int in Pandas
- 5 Methods to change columns type in Pandas
- Convert string column to datetime in Pandas
- Convert Multiple columns to datetime in Pandas
- Convert multiple float columns to int Python pandas
2. How to convert float to datetime in Pandas Dataframe
The given dataframe column ‘Admission_date’ contains the value of date as float64 type of format yyddmm. To convert float date value to datetime the Pandas dataframe pd.to_datetime() function is used that takes the first parameter is the column name, the Second parameter is the format of date in which, need to convert. We have passed the date format ‘%y%m%d’ which is equal to yyyymmdd.
import pandas as pd
import numpy as np
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Subject': ['Math', 'Math', 'Math', 'Phy'],
'Admission_date':[22615.0,22617.0,22616.0,22618.0]
}
df = pd.DataFrame(data)
print('Existing datatype:\n',df.dtypes)
df['Admission_date'] = pd.to_datetime(df['Admission_date'],format='%y%m%d')
print('\n',df)
print('datatype after conversion:',df.dtypes)
Output
Existing datatype:
Name object
Marks int64
Subject object
Admission_date datetime64[ns]
dtype: object
Name Marks Subject Admission_date
0 Jack 97 Math 2022-05-07 03:06:09
1 Rack 97 Math 2022-05-05 09:10:12
2 Max 100 Math 2022-03-06 02:02:22
3 David 100 Phy 2022-06-06 07:07:07
datatype after conversio: Name object
Marks int64
Subject object
Admission_date datetime64[ns]
dtype: object
3. How to convert float to datetime in Pandas Dataframe
The dataframe column ‘Admission_date’ contains the date and time value as float64 in format yyyymmddhhmm:ss. to convert float value to datetime without losing the time value, we will have to pass the format value “%y%m%d%H%M%S” to the to_datetime() method of Pandas dataframe that is equal to yyyymmdd hh:mm: ss. Sometimes the float date value contains time in microseconds, the format used for microseconds is “%y%m%d%H%M%S%F”.
import pandas as pd
import numpy as np
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Subject': ['Math', 'Math', 'Math', 'Phy'],
'Admission_date':[2207150634.0,2205151236.0,2206130169.0,2206160337.0]
}
df = pd.DataFrame(data)
print('Existing datatype:\n',df.dtypes)
df['Admission_date'] = pd.to_datetime(df['Admission_date'],format='%y%m%d%H%M%S')
print('\n',df)
print('datatype after conversion:',df.dtypes)
Output
Existing datatype:
Name object
Marks int64
Subject object
Admission_date float64
dtype: object
Name Marks Subject Admission_date
0 Jack 97 Math 2022-07-15 06:03:04
1 Rack 97 Math 2022-05-15 12:03:06
2 Max 100 Math 2022-06-13 01:06:09
3 David 100 Phy 2022-06-16 03:03:07
datatype after conversion: Name object
Marks int64
Subject object
Admission_date datetime64[ns]
dtype: object
4.How to convert float to datetime Pandas Dataframe
In this Python program, We have performed the same operation as we have done in the above example instead of a simple approach we have used lambda and applied the method of pandas dataframe to convert float64 to data time. It is a short and one-line code.
df = pd.DataFrame(data)
print('Existing datatype:\n',df .dtypes)
df['Admission_date'] = df['Admission_date'].apply(lambda x: pd.to_datetime(x, format='%y%m%d%H%M%S'))
print(df)
print('\n datatype after conversio:\n',df .dtypes)
Output
Existing datatype:
Name object
Marks int64
Subject object
Admission_date float64
dtype: object
Name Marks Subject Admission_date
0 Jack 97 Math 2022-07-15 06:03:04
1 Rack 97 Math 2022-05-15 12:03:06
2 Max 100 Math 2022-06-13 01:06:09
3 David 100 Phy 2022-06-16 03:03:07
datatype after conversio:
Name object
Marks int64
Subject object
Admission_date datetime64[ns]
dtype: object