Formating Timestamp in Pandas dataframe

I have a large dataset representing an electrical signal (as an array) that need's to be passed as a pandas dataframe to another function. The thing is the dataframe has to have an index with a freq attribute, and the original dataset doesn't have a timestamp, 1d array with the samples, but I know the sample freq (86Hz) so I can assign a timestamp for each sample:

>>>a = [1,2,3,4,5,6]
>>>b = []
>>>j=0
>>>for i in a:
>>>    b.append(round(j, 3))
>>>    j = j+(1/86)
>>>c = np.c_[b, a]
     ([[ 0.   ,  1.   ],
       [ 0.012,  2.   ],
       [ 0.023,  3.   ],
       [ 0.035,  4.   ],
       [ 0.047,  5.   ],
       [ 0.058,  6.   ]])

Then I turn it in to a dataframe:

d = pd.DataFrame(data=c[0:,1], index=c[0:,0])

        0
0.000   1.0
0.012   2.0
0.023   3.0
0.035   4.0
0.047   5.0
0.058   6.0

The problem is the index has no freq attribute, I think it's a formating issue, but not sure, and after a loot of googling I didn't find anything.

When I write

d.index.freq

it should return 86, but gives me an AttributeError: 'Float64Index' object has no attribute 'freq'

Thanks in advance!

By the way, the timestamp is in seconds, starting with the first sample... or that is my intent...

1 answer

  • answered 2018-01-11 20:59 MaxU

    If I understand correctly you can do it like this:

    In [109]: d.index = pd.timedelta_range(d.index.min(), periods=len(d), freq='86L')
    
    In [110]: d
    Out[110]:
                       0
    00:00:00         1.0
    00:00:00.086000  2.0
    00:00:00.172000  3.0
    00:00:00.258000  4.0
    00:00:00.344000  5.0
    00:00:00.430000  6.0
    
    In [111]: d.index.freq
    Out[111]: <86 * Millis>
    
    In [112]: d.index.dtype
    Out[112]: dtype('<m8[ns]')
    
    In [113]: d.index.dtype_str
    Out[113]: 'timedelta64[ns]'