upsampling multiple time series in one dataframe

thatsnotmyname

got data like that in a csv

id,date,price
A,2020-05-04 15:00:00,1.2
B,2020-05-04 15:05:00,1.3
A,2020-05-04 16:07:00,1.4
B,2020-05-04 16:07:00,1.5

so there is a time series for A and another one for B in same DataFrame. I created dataframe and put index like df.set_index(["id","date"])

now I want to upsample to minute so that result corresponds to a csv looking like that:

id,date,price
A,2020-05-04 15:00:00,1.2
A,2020-05-04 15:01:00,1.2
...
A,2020-05-04 16:06:00,1.2
A,2020-05-04 16:07:00,1.4
B,2020-05-04 15:05:00,1.3
B,2020-05-04 15:06:00,1.3
...
B,2020-05-04 16:06:00,1.3
B,2020-05-04 16:07:00,1.5

but dont get that df.resample("60S", level ="date").pad() thing going, getting error that I can only use it for "Datetimeindex but it is Multiindex" when omitting level and putting level getting "Upsampling from level= or on= selection is not supported, use .set_index to explicitly set index to datetime-like" error...

I feel there must be a simple solution...?inst it very common to have multiple instances of a series in one dataset? For sure I could split dataframe and combine thereafter... There is also some post using grouper but that to me sounds overly complex, to it sounds like level parameter must be the solution somehow...

jezrael

Create DatetimeIndex and use DataFrame.groupby chained with DataFrameGroupBy.resample:

df = df.set_index("date").groupby('id').resample("60S").pad()
print (df)
                       id  price
id date                         
A  2020-05-04 15:00:00  A    1.2
   2020-05-04 15:01:00  A    1.2
   2020-05-04 15:02:00  A    1.2
   2020-05-04 15:03:00  A    1.2
   2020-05-04 15:04:00  A    1.2
                   ..    ...
B  2020-05-04 16:03:00  B    1.3
   2020-05-04 16:04:00  B    1.3
   2020-05-04 16:05:00  B    1.3
   2020-05-04 16:06:00  B    1.3
   2020-05-04 16:07:00  B    1.5

[131 rows x 2 columns]

For processing only price column use:

df1 = df.set_index("date").groupby('id')['price'].resample("60S").pad().reset_index()
print (df1)
    id                date  price
0    A 2020-05-04 15:00:00    1.2
1    A 2020-05-04 15:01:00    1.2
2    A 2020-05-04 15:02:00    1.2
3    A 2020-05-04 15:03:00    1.2
4    A 2020-05-04 15:04:00    1.2
..  ..                 ...    ...
126  B 2020-05-04 16:03:00    1.3
127  B 2020-05-04 16:04:00    1.3
128  B 2020-05-04 16:05:00    1.3
129  B 2020-05-04 16:06:00    1.3
130  B 2020-05-04 16:07:00    1.5

[131 rows x 3 columns]

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to remove inconsistencies from dataframe (time series)

分類Dev

Expand time series data in pandas dataframe

分類Dev

format a time series as dataframe with julian date

分類Dev

How to convert to a time series and plot a dataframe with each day as a variable or column?

分類Dev

How to convert to a time series and plot a dataframe with each day as a variable or column?

分類Dev

Python pandas: insert rows for missing dates, time series in groupby dataframe

分類Dev

How to plot beautifully the segmentation of time series (pandas dataframe)

分類Dev

Using optim to estimate parameters from multiple time series with different constants

分類Dev

Slice Pandas series (based on index) into multiple columns of dataframe

分類Dev

Complicated dataframe transformation with moving multiple columns into one

分類Dev

Finding historical seasonal average for given month in a monthly series in a dataframe time-series

分類Dev

Add multiple dataframes to one dataframe without overwriting the existing dataframe in R

分類Dev

jQuery fadeOut multiple divs one by one, then fadeIn all at the same time

分類Dev

Performing pct_change() that only considers the prior year in a time-series dataframe?

分類Dev

how can i replace time-series dataframe specific values in pandas?

分類Dev

Add multiple actions to a Core - Data relationship at one time

分類Dev

How to plot multiple time series with a reverse barplot on the secondary axis using ggplot2?

分類Dev

ElasticSearch for Time Series Data

分類Dev

Disaggregate in the context of a time series

分類Dev

Time Series "status" calculation

分類Dev

Time Series by group

分類Dev

for loop in irregular time series

分類Dev

R: time series with value

分類Dev

Reshape of Time series in R

分類Dev

Pandas combinations of Dataframe and series

分類Dev

merging time series datasets, but missing the time series column

分類Dev

merging time series datasets, but missing the time series column

分類Dev

Time-series boxplot in pandas

分類Dev

What is time-series database?

Related 関連記事

  1. 1

    How to remove inconsistencies from dataframe (time series)

  2. 2

    Expand time series data in pandas dataframe

  3. 3

    format a time series as dataframe with julian date

  4. 4

    How to convert to a time series and plot a dataframe with each day as a variable or column?

  5. 5

    How to convert to a time series and plot a dataframe with each day as a variable or column?

  6. 6

    Python pandas: insert rows for missing dates, time series in groupby dataframe

  7. 7

    How to plot beautifully the segmentation of time series (pandas dataframe)

  8. 8

    Using optim to estimate parameters from multiple time series with different constants

  9. 9

    Slice Pandas series (based on index) into multiple columns of dataframe

  10. 10

    Complicated dataframe transformation with moving multiple columns into one

  11. 11

    Finding historical seasonal average for given month in a monthly series in a dataframe time-series

  12. 12

    Add multiple dataframes to one dataframe without overwriting the existing dataframe in R

  13. 13

    jQuery fadeOut multiple divs one by one, then fadeIn all at the same time

  14. 14

    Performing pct_change() that only considers the prior year in a time-series dataframe?

  15. 15

    how can i replace time-series dataframe specific values in pandas?

  16. 16

    Add multiple actions to a Core - Data relationship at one time

  17. 17

    How to plot multiple time series with a reverse barplot on the secondary axis using ggplot2?

  18. 18

    ElasticSearch for Time Series Data

  19. 19

    Disaggregate in the context of a time series

  20. 20

    Time Series "status" calculation

  21. 21

    Time Series by group

  22. 22

    for loop in irregular time series

  23. 23

    R: time series with value

  24. 24

    Reshape of Time series in R

  25. 25

    Pandas combinations of Dataframe and series

  26. 26

    merging time series datasets, but missing the time series column

  27. 27

    merging time series datasets, but missing the time series column

  28. 28

    Time-series boxplot in pandas

  29. 29

    What is time-series database?

ホットタグ

アーカイブ