Change Date Format To Int In Python Pandas
I have several types of date in Python Pandas. 1. 17/12/04 14:19:48.374835 < class 'str' > 2. 20100202072111 < class 'numpy.int64'> 3. 2.017120e+11 < class 'numpy.
Solution 1:
You have to convert your dates to string and specify the format for each file. Use strptime:
from datetime import datetime
date = datetime.strptime("17/12/04 14:19:48.374835", "%y/%m/%d %H:%M:%S.%f")
To convert it into int you can use int() and strftime()
date_number = int(datetime.strftime(date, "%Y%m%d%H%M%S").replace("/", ""))
print(date_number)
I hope this helps you with your problem.
Edit: Example with dataframe:
import pandas as pd
from datetime import datetime
data = ["17/12/04 14:19:48.374835", "19/11/05 15:20:48.374835"]
df = pd.DataFrame(data, columns=['Timestamp'])
#this replaces the datetime with a string in your 1. dataframefor idx, row in df.itertuples(name='Timestamp'):
date = datetime.strptime(row, "%y/%m/%d %H:%M:%S.%f")
date_number = int(datetime.strftime(date, "%Y%m%d%H%M%S").replace("/", ""))
df.loc[idx, 'Timestamp'] = date_number
Do this for every dataframe and format you have, or write a function that checks the format and converts it to int for example.
I guess you can take it from there :)
Post a Comment for "Change Date Format To Int In Python Pandas"