In Python I'm trying to format my date column in a dataframe to float object format. I end up getting Timestamp format instead.
In:
X = []
for row in data:
date = pd.to_datetime(row[0], format='%Y/%m/%d')
X.append(date)
print(X)
Out:
[Timestamp('2008-01-02 00:00:00'),
Timestamp('2009-01-02 00:00:00'),
Timestamp('2010-01-04 00:00:00'),
Timestamp('2011-01-03 00:00:00'),
Timestamp('2012-01-03 00:00:00'),
Timestamp('2013-12-02 00:00:00'),
Timestamp('2014-12-01 00:00:00'),
Timestamp('2015-01-02 00:00:00'),
Timestamp('2016-01-04 00:00:00'),
Timestamp('2017-01-03 00:00:00'),
Timestamp('2018-01-02 00:00:00'),
Timestamp('2019-01-02 00:00:00'),
Timestamp('2020-01-02 00:00:00')]
How can I change to float object instead of Timestamp? Should I be using datetime.strptime instead?
If you need dates in numeric format, use:
import datetime
dt = datetime.datetime.strptime('2020/03/08', '%Y/%m/%d')
timestamp = dt.replace(tzinfo=datetime.timezone.utc).timestamp()
Dates will be displayed as UNIX timestamps (float type)
Edit: if you want to use pandas, see also pandas datetime to unix timestamp seconds