1

I have a dataframe (df) with the index as datetime index in "%Y-%m-%d %H:%M:%S.%f" format i.e 2012-06-16 15:53:42.457000.

I am trying to create groups of 1 second using groupby method i.e

x= df.groupby(pd.TimeGrouper('1S'))
time=x.first().index[1]
print time

The problem is using groupby method i am only getting the timestamp in seconds only i.e "2012-06-16 15:53:42" , the milliseconds are excluded. Is there a way to get the complete timestamp? thank you

Levon
  • 138,105
  • 33
  • 200
  • 191
user2083529
  • 715
  • 3
  • 13
  • 25

2 Answers2

1

I think this is a problem of formatting.

> df.index[0].strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
'2016-01-01 00:00:00.000'

Documentation here

Guillaume Thomas
  • 2,220
  • 2
  • 24
  • 33
  • it's not the formatting problem. Ialready tried that. Normally i get the complete time in miliseconds if i use df.index [0]. But after grouping i am getting time till seconds without miliseconds. – user2083529 Jul 07 '16 at 12:28
0

After spending few hours, i solved it. The functions such as groupby, rolling etc were giving the same problem. These functions uses a general datetime index for grouping which is mathematically the multiples of the defined frequency. There are possibilities to get keyerror if that index is used to get data of dataframe.

To get the complete datetime index (in milliseconds), access the daytime index of members of the individual group which were created by groupby method. i.e

time=  x2.first()['timeindex'][0]

where timeindex is the datetime index in my dataframe. [0] is the datetime index of the first member of the group, it can be incremented to get the datetime index of the second, 3rd and so on members of each groups.

user2083529
  • 715
  • 3
  • 13
  • 25