pandas dataframe group by uneven timestamp

bbc

I have a dataframe of market prices, timestamps are in microsecond, eg.

Time       Bid
0  2014-03-03 23:30:30.224323  0.892500
1  2014-03-03 23:30:30.224390  0.892525
2  2014-03-03 23:30:30.224408  0.892525
3  2014-03-03 23:30:30.364299  0.892525
4  2014-03-03 23:30:31.022652  0.892500
5  2014-03-03 23:30:31.022702  0.892525
6  2014-03-03 23:30:31.866949  0.892525
7  2014-03-03 23:30:33.366843  0.892525
8  2014-03-03 23:30:33.858239  0.892525
9  2014-03-03 23:30:34.360997  0.892525
10 2014-03-03 23:30:35.034307  0.892525
11 2014-03-03 23:30:36.110848  0.892525
12 2014-03-03 23:30:36.359973  0.892525
13 2014-03-03 23:30:38.111191  0.892525
14 2014-03-03 23:30:41.599924  0.892525
15 2014-03-03 23:30:41.599972  0.892500

How do I group by Time by strip away microsecond, for example, how to convert a OHLC(open, high, low, close) structure of evenly time slot(1min, 5mins, 1hour etc), and also counts for each time slot? I tried to add another column with

e['Time2'] = pd.to_datetime(e.Time, format='%Y/%m/%d %H:%M:%S')

dropping %f, but Time2 column looks identical to Time column.

Many thanks,

how to convert it into something like

Andy Hayden

You need to make the datetime the index so that you can resample:

In [11]: df = df.set_index('Time')

In [12]: df.resample('5min', how='ohlc')
Out[12]:
                        Bid
                       open      high     low   close
Time
2014-03-03 23:30:00  0.8925  0.892525  0.8925  0.8925

Note: Unfortunately to_datetime doesn't raise if it fails (at least by default) so it often a good idea to check the dtype of the column is datetime64...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

From Dev

Failing to convert Pandas dataframe timestamp

From Dev

How to fetch timestamp in Pandas DataFrame

From Dev

How to fetch timestamp in Pandas DataFrame

From Dev

group by in Pandas DataFrame Python

From Dev

Group by fields in pandas dataframe

From Dev

How to group sum along timestamp in pandas?

From Dev

Python: reduce precision pandas timestamp dataframe

From Dev

Truncate `TimeStamp` column to hour precision in pandas `DataFrame`

From Dev

Can't index by timestamp in pandas dataframe

From Dev

Setting timezone for timestamp data in pandas dataframe

From Dev

Convert dataframe with odd timestamp to timeseries with pandas

From Dev

Search for elements by timestamp in a sorted pandas dataframe

From Dev

Find row corresponding to timestamp in pandas DataFrame

From Dev

Convert dataframe with odd timestamp to timeseries with pandas

From Dev

Setting timezone for timestamp data in pandas dataframe

From Dev

Slice DataFrame using pandas Timestamp on DatetimeIndex

From Dev

issues with pandas dataframe insert to mysql with timestamp

From Dev

How do you fill uneven pandas dataframe column with first value in column

From Java

Group pandas dataframe in unusual way

From Dev

Multiple aggregation in group by in Pandas Dataframe

From Dev

Calculation within Pandas dataframe group

From Dev

Group by groups to Pandas Series/Dataframe

From Dev

Group by continuous indexes in Pandas DataFrame

From Java

group by pandas dataframe and select latest in each group

From Dev

group by pandas dataframe and select latest in each group

From Dev

Pandas - Rolling window - uneven interval

From Dev

Pandas calculate result dataframe from a dataframe of multiple trades at same timestamp

From Dev

Python Pandas: Groupby date, and accessing each group by timestamp

Related Related

  1. 1

    How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

  2. 2

    Failing to convert Pandas dataframe timestamp

  3. 3

    How to fetch timestamp in Pandas DataFrame

  4. 4

    How to fetch timestamp in Pandas DataFrame

  5. 5

    group by in Pandas DataFrame Python

  6. 6

    Group by fields in pandas dataframe

  7. 7

    How to group sum along timestamp in pandas?

  8. 8

    Python: reduce precision pandas timestamp dataframe

  9. 9

    Truncate `TimeStamp` column to hour precision in pandas `DataFrame`

  10. 10

    Can't index by timestamp in pandas dataframe

  11. 11

    Setting timezone for timestamp data in pandas dataframe

  12. 12

    Convert dataframe with odd timestamp to timeseries with pandas

  13. 13

    Search for elements by timestamp in a sorted pandas dataframe

  14. 14

    Find row corresponding to timestamp in pandas DataFrame

  15. 15

    Convert dataframe with odd timestamp to timeseries with pandas

  16. 16

    Setting timezone for timestamp data in pandas dataframe

  17. 17

    Slice DataFrame using pandas Timestamp on DatetimeIndex

  18. 18

    issues with pandas dataframe insert to mysql with timestamp

  19. 19

    How do you fill uneven pandas dataframe column with first value in column

  20. 20

    Group pandas dataframe in unusual way

  21. 21

    Multiple aggregation in group by in Pandas Dataframe

  22. 22

    Calculation within Pandas dataframe group

  23. 23

    Group by groups to Pandas Series/Dataframe

  24. 24

    Group by continuous indexes in Pandas DataFrame

  25. 25

    group by pandas dataframe and select latest in each group

  26. 26

    group by pandas dataframe and select latest in each group

  27. 27

    Pandas - Rolling window - uneven interval

  28. 28

    Pandas calculate result dataframe from a dataframe of multiple trades at same timestamp

  29. 29

    Python Pandas: Groupby date, and accessing each group by timestamp

HotTag

Archive