Pandas: how to use between_time with milliseconds?
Consider this:
import pandas as pd
import numpy as np
idx2=[pd.to_datetime('2016-08-31 22:08:12.000'),
pd.to_datetime('2016-08-31 22:08:12.200'),
pd.to_datetime('2016-08-31 22:08:12.400')]
test=pd.DataFrame({'value':[1,1,3], 'groups' : ['A',np.NaN,'A']},index=idx2)
test
Out[27]:
groups value
2016-08-31 22:08:12.000 A 1
2016-08-31 22:08:12.200 NaN 1
2016-08-31 22:08:12.400 A 3
I only need to store data between 22:08:12.200
and 22:08:12.400
, so I naturally use between_time
:
test.between_time('22:08:12.200','22:08:12.400')
gives
ValueError: Unable to convert arg ['22: 08: 12.200 '] over time
What's wrong here? How can I slice mine dataframe
based on time with millisecond information?
source to share
I'm not sure why the straight string isn't working, but it looks like it has to do with the time conversion from datetime
that came from the string. But you can do without the explicit conversion to time
like:
Code:
test.between_time(*pd.to_datetime(['22:08:12.200', '22:08:12.400']).time)
Test code:
import pandas as pd
import numpy as np
idx2 = [
pd.to_datetime('2016-08-31 22:08:12.000'),
pd.to_datetime('2016-08-31 22:08:12.200'),
pd.to_datetime('2016-08-31 22:08:12.400')]
test = pd.DataFrame(
{'value': [1, 1, 3], 'groups': ['A', np.NaN, 'A']}, index=idx2)
print(test.between_time(
*pd.to_datetime(['22:08:12.200', '22:08:12.400']).time))
Results:
groups value
2016-08-31 22:08:12.200 NaN 1
2016-08-31 22:08:12.400 A 3
source to share