0

I have the following pandas series data::

print(times)

gives

1        2017-07-23T00:26:50Z
3        2017-07-31T04:07:24Z
6        2017-07-18T15:09:20Z
8        2017-07-12T05:24:14Z
...
12353    2017-07-12T18:34:48Z
12355    2017-08-01T22:27:42Z
12356    2017-07-11T03:36:44Z

and I simply want to convert these UTC timestamps into seconds or

from astropy.time import Time
t = pd.Timestamp(times)

just gives a

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-320d28559d92> in <module>()
----> 1 t = pd.Timestamp(times)
pandas/_libs/tslibs/timestamps.pyx in 
pandas._libs.tslibs.timestamps.Timestamp.__new__()
pandas/_libs/tslibs/conversion.pyx in 
pandas._libs.tslibs.conversion.convert_to_tsobject()

TypeError: Cannot convert input [1        2017-07-23T00:26:50Z

I really don't understand what is going on.

npross
  • 1,756
  • 6
  • 19
  • 38

1 Answers1

0

You can use Pandas DateTime capabilities:

import pandas as pd

import pandas as pd
s=pd.Series(['2017-07-23T00:26:50Z', '2017-07-31T04:07:24Z', '2017-07-18T15:09:20Z', '2017-07-12T18:34:48Z'])

pd.to_datetime(s)

P.S. - those "extra numbers" are just the row numbers/ automatic index pandas assigns when you read in the data :)

EHB
  • 1,127
  • 3
  • 13
  • 24
  • This doesn't work. I can do *one* datetime as above, but giving to_datetime the whole series causes the crash – npross Oct 19 '18 at 16:44
  • You should add a reproducible example of your problem. The code above works just fine... if my solution above doesn't work for you, I bet there is a strange item somewhere in your list; I'd suggest looping through and seeing where it's throwing an error. People have a hard time helping if they can't re-create the problem themselves to solve :) – EHB Oct 19 '18 at 18:49
  • I honestly don't know how to give you a reproducible example of the problem without e.g. linking the code and data to a webpage/GitHub. – npross Oct 19 '18 at 19:09
  • Maybe something here: https://stackoverflow.com/questions/52413246/how-do-i-provide-a-reproducible-copy-of-my-existing-dataframe; or https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples. – EHB Oct 19 '18 at 19:13
  • Also, you're not using pandas datetime in your original question code... so try using that and see if it fixes things (you're using `pd.Timestamp`) – EHB Oct 19 '18 at 19:15
  • Okay, I've made a GitHub repo with the code and data here:: https://github.com/d80b2t/StackOverflow/tree/master/52884357 Please have a look and see that the code still spits out a TypeError: Cannot convert input [0 error. – npross Oct 19 '18 at 20:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182169/discussion-between-ehb-and-npross). You're still using `t = pd.Timestamp(times)`. You want `pd.to_datetime()` – EHB Oct 19 '18 at 20:07