Search code examples
pythonpandastimestampunix-timestamputc

Converting UTC timestamps to seconds TypeError


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.


Solution

  • 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 :)