0

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?

4
  • t = date.apply(timetuple)NameError: name 'timetuple' is not defined Commented Mar 5, 2016 at 7:38
  • Ah, no coffee yet this morning, should probably be t = date.apply(lambda x: x.timetuple()) Sadly don't have pandas installed to test on this machine :) Commented Mar 5, 2016 at 7:42
  • sorry( after print time.mktime(t) TypeError: argument must be sequence of length 9, not 1400 Commented Mar 5, 2016 at 7:55
  • Welcome to Stack Overflow! It is unclear what code exactly you execute leading to the error. Please provide Minimal, Complete, Verifiable Example that demonstrates your problem would help you get better answers. Thanks! Commented Mar 5, 2016 at 8:14

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

1 Comment

where can i send coffe?)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.