Edward
Edward

Reputation: 4623

Convert Data to float

I have a variable as below

              Date
3001   2014-04-16 12:02:00
3002   2014-03-07 08:42:00
3003   2014-11-09 11:28:00
3004   2014-11-09 11:51:00
3005   2014-09-27 10:12:00
3006   2014-10-15 23:18:00
3007   2014-02-12 20:27:00
3008   2015-03-27 18:37:00
3009   2015-06-04 18:34:00
Name: Date, dtype: datetime64[ns]

I want to convert to float like this

import pandas as pd 
import time
from datetime import datetime
df = pd.to_datetime("2014-04-16 12:02:00")
t = df.timetuple()
print time.mktime(t)
1397638920.0

It's correct way, but when i want to do for the variable i get

date = df['Date']
t = date.timetuple()
AttributeError: 'Series' object has no attribute 'timetuple'

df['Date'] - dtype: datetime64[ns] Where is a mistake?

Upvotes: 0

Views: 317

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180867

You need to use apply over the series to run a function on each element instead of just calling functions on the series itself;

t = date.apply(lambda x: x.timetuple())

# 0     (2014, 4, 16, 12, 2, 0, 2, 106, -1)
# 1     (2014, 3, 7, 8, 42, 0, 4, 66, -1)
# 2     (2014, 11, 9, 11, 28, 0, 6, 313, -1)
# 3     (2014, 11, 9, 11, 51, 0, 6, 313, -1)
# 4     (2014, 9, 27, 10, 12, 0, 5, 270, -1)
# ...

print t.apply(lambda x:time.mktime(x))

# 0    1397642520
# 1    1394178120
# 2    1415528880
# 3    1415530260
# 4    1411805520

Upvotes: 1

Related Questions