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

jerry han

I have the dataframes below (date/time is multi index) and I want to replace column values in (00:00:00~07:00:00) as a numpy array:

[[ 21.63920663  21.62012822  20.9900515   21.23217008  21.19482458
   21.10839656  20.89631935  20.79977166  20.99176729  20.91567565
   20.87258765  20.76210464  20.50357827  20.55897631  20.38005033
   20.38227309  20.54460993  20.37707293  20.08279925  20.09955877
   20.02559575  20.12390737  20.2917257   20.20056711  20.1589065
   20.41302289  20.48000767  20.55604102  20.70255192]]
     date        time    
2018-01-26  00:00:00    21.65
            00:15:00      NaN
            00:30:00      NaN
            00:45:00      NaN
            01:00:00      NaN
            01:15:00      NaN
            01:30:00      NaN
            01:45:00      NaN
            02:00:00      NaN
            02:15:00      NaN
            02:30:00      NaN
            02:45:00      NaN
            03:00:00      NaN
            03:15:00      NaN
            03:30:00      NaN
            03:45:00      NaN
            04:00:00      NaN
            04:15:00      NaN
            04:30:00      NaN
            04:45:00      NaN
            05:00:00      NaN
            05:15:00      NaN
            05:30:00      NaN
            05:45:00      NaN
            06:00:00      NaN
            06:15:00      NaN
            06:30:00      NaN
            06:45:00      NaN
            07:00:00      NaN
            07:15:00      NaN
            07:30:00      NaN
            07:45:00      NaN
            08:00:00      NaN
            08:15:00      NaN
            08:30:00      NaN
            08:45:00      NaN
            09:00:00      NaN
            09:15:00      NaN
            09:30:00      NaN
            09:45:00      NaN
            10:00:00      NaN
            10:15:00      NaN
            10:30:00      NaN
            10:45:00      NaN
            11:00:00      NaN
Name: temp, dtype: float64
<class 'datetime.time'>

How can I do this?

jezrael

You can use slicers:

idx = pd.IndexSlice
df1.loc[idx[:, '00:00:00':'02:00:00'],:] = 1

Or if second levels are times:

import datetime

idx = pd.IndexSlice
df1.loc[idx[:, datetime.time(0, 0, 0):datetime.time(2, 0, 0)],:] = 1

Sample:

print (df1)
                       aaa
date       time           
2018-01-26 00:00:00  21.65
           00:15:00    NaN
           00:30:00    NaN
           00:45:00    NaN
           01:00:00    NaN
           01:15:00    NaN
           01:30:00    NaN
           01:45:00    NaN
           02:00:00    NaN
           02:15:00    NaN
           02:30:00    NaN
           02:45:00    NaN
           03:00:00    NaN
2018-01-27 00:00:00   2.00
           00:15:00    NaN
           00:30:00    NaN
           00:45:00    NaN
           01:00:00    NaN
           01:15:00    NaN
           01:30:00    NaN
           01:45:00    NaN
           02:00:00    NaN
           02:15:00    NaN
           02:30:00    NaN
           02:45:00    NaN
           03:00:00    NaN

idx = pd.IndexSlice
df1.loc[idx[:, '00:00:00':'02:00:00'],:] = 1
print (df1)
                     aaa
date       time         
2018-01-26 00:00:00  1.0
           00:15:00  1.0
           00:30:00  1.0
           00:45:00  1.0
           01:00:00  1.0
           01:15:00  1.0
           01:30:00  1.0
           01:45:00  1.0
           02:00:00  1.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN
2018-01-27 00:00:00  1.0
           00:15:00  1.0
           00:30:00  1.0
           00:45:00  1.0
           01:00:00  1.0
           01:15:00  1.0
           01:30:00  1.0
           01:45:00  1.0
           02:00:00  1.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN

EDIT:

For assign array is necessary use numpy.tile for repeat by length of first level unique values:

df1.loc[idx[:, '00:00:00':'02:00:00'],:] = np.tile(np.arange(1, 10),len(df1.index.levels[0]))
print (df1)
                     aaa
date       time         
2018-01-26 00:00:00  1.0
           00:15:00  2.0
           00:30:00  3.0
           00:45:00  4.0
           01:00:00  5.0
           01:15:00  6.0
           01:30:00  7.0
           01:45:00  8.0
           02:00:00  9.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN
2018-01-27 00:00:00  1.0
           00:15:00  2.0
           00:30:00  3.0
           00:45:00  4.0
           01:00:00  5.0
           01:15:00  6.0
           01:30:00  7.0
           01:45:00  8.0
           02:00:00  9.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN

More general solution with generated array by length of slice:

idx = pd.IndexSlice
len0 = df1.loc[idx[df1.index.levels[0][0], '00:00:00':'02:00:00'],:].shape[0]
len1 = len(df1.index.levels[0])
df1.loc[idx[:, '00:00:00':'02:00:00'],:] = np.tile(np.arange(1, len0 + 1), len1)

Tested with times:

import datetime
idx = pd.IndexSlice
arr =np.tile(np.arange(1, 10),len(df1.index.levels[0]))
df1.loc[idx[:, datetime.time(0, 0, 0):datetime.time(2, 0, 0)],:] = arr
print (df1)
                     aaa
date       time         
2018-01-26 00:00:00  1.0
           00:15:00  2.0
           00:30:00  3.0
           00:45:00  4.0
           01:00:00  5.0
           01:15:00  6.0
           01:30:00  7.0
           01:45:00  8.0
           02:00:00  9.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN
2018-01-27 00:00:00  1.0
           00:15:00  2.0
           00:30:00  3.0
           00:45:00  4.0
           01:00:00  5.0
           01:15:00  6.0
           01:30:00  7.0
           01:45:00  8.0
           02:00:00  9.0
           02:15:00  NaN
           02:30:00  NaN
           02:45:00  NaN
           03:00:00  NaN

EDIT:

Last was problem found - my solution wokrs with one column DataFrame, but if working with Series need remove one ::

arr = np.array([[ 21.63920663, 21.62012822, 20.9900515, 21.23217008, 21.19482458, 21.10839656, 
                 20.89631935, 20.79977166, 20.99176729, 20.91567565, 20.87258765, 20.76210464,
                 20.50357827, 20.55897631, 20.38005033, 20.38227309, 20.54460993, 20.37707293, 
                 20.08279925, 20.09955877, 20.02559575, 20.12390737, 20.2917257, 20.20056711, 
                 20.1589065, 20.41302289, 20.48000767, 20.55604102, 20.70255192]])

import datetime
idx = pd.IndexSlice
df1.loc[idx[:, datetime.time(0, 0, 0): datetime.time(7, 0, 0)]] = arr[0]
                                                          ---^^^

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Replace negative values in pandas Series

分類Dev

How can I plot specific attributes rather than default of all attributes in Time Series

分類Dev

R How can I use the apply function to a time series object and keep the dates attached to the specific columns?

分類Dev

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

分類Dev

How can I replace each and every word with 3 values from another dataframe

分類Dev

How can I replace the NULL values in dataframe with Average of Forward and backward fill?

分類Dev

In pandas DataFrame, how can I store a specific value from a column into a variable, and then subsequently remove that value from the column?

分類Dev

How to replace certain rows by shared column values in pandas DataFrame?

分類Dev

how can I add different size of the values into a pandas data frame at a time

分類Dev

How can I slice elements of one Pandas dataframe column by different values?

分類Dev

How to put specific dictionary values in dataframe columns (pandas)

分類Dev

Expand time series data in pandas dataframe

分類Dev

How can I return common values of dictionary values for specific keys?

分類Dev

How can I group a sorted pandas.Series?

分類Dev

How can I replace multiple rows simultaneously in a Python dataframe?

分類Dev

How to merge pandas calculated series into pandas dataframe

分類Dev

Replace and mapping string values in a Python dataframe with pandas

分類Dev

Replace column values in large Pandas dataframe

分類Dev

How can I make the values of this dataframe add progressively. Ex. the second is equal to the first + second and so on, as continuous time

分類Dev

How can i replace the values in respect with with missing data with Zero?

分類Dev

How can I replace values in a Transition layer? (gdistance)

分類Dev

How can I find and replace values between two dataframes in R

分類Dev

How i can create dataframe of specific interval columns by index number

分類Dev

How to extract values from a Pandas DataFrame, rather than a Series (without referencing the index)?

分類Dev

How to remove inconsistencies from dataframe (time series)

分類Dev

How can i match values inside the same dataframe?

分類Dev

How to update series based on other pandas dataframe

分類Dev

How can I change this form of dictionary to pandas dataframe?

分類Dev

How can I dynamically create '&' filters of varying length for pandas DataFrame

Related 関連記事

  1. 1

    Replace negative values in pandas Series

  2. 2

    How can I plot specific attributes rather than default of all attributes in Time Series

  3. 3

    R How can I use the apply function to a time series object and keep the dates attached to the specific columns?

  4. 4

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

  5. 5

    How can I replace each and every word with 3 values from another dataframe

  6. 6

    How can I replace the NULL values in dataframe with Average of Forward and backward fill?

  7. 7

    In pandas DataFrame, how can I store a specific value from a column into a variable, and then subsequently remove that value from the column?

  8. 8

    How to replace certain rows by shared column values in pandas DataFrame?

  9. 9

    how can I add different size of the values into a pandas data frame at a time

  10. 10

    How can I slice elements of one Pandas dataframe column by different values?

  11. 11

    How to put specific dictionary values in dataframe columns (pandas)

  12. 12

    Expand time series data in pandas dataframe

  13. 13

    How can I return common values of dictionary values for specific keys?

  14. 14

    How can I group a sorted pandas.Series?

  15. 15

    How can I replace multiple rows simultaneously in a Python dataframe?

  16. 16

    How to merge pandas calculated series into pandas dataframe

  17. 17

    Replace and mapping string values in a Python dataframe with pandas

  18. 18

    Replace column values in large Pandas dataframe

  19. 19

    How can I make the values of this dataframe add progressively. Ex. the second is equal to the first + second and so on, as continuous time

  20. 20

    How can i replace the values in respect with with missing data with Zero?

  21. 21

    How can I replace values in a Transition layer? (gdistance)

  22. 22

    How can I find and replace values between two dataframes in R

  23. 23

    How i can create dataframe of specific interval columns by index number

  24. 24

    How to extract values from a Pandas DataFrame, rather than a Series (without referencing the index)?

  25. 25

    How to remove inconsistencies from dataframe (time series)

  26. 26

    How can i match values inside the same dataframe?

  27. 27

    How to update series based on other pandas dataframe

  28. 28

    How can I change this form of dictionary to pandas dataframe?

  29. 29

    How can I dynamically create '&' filters of varying length for pandas DataFrame

ホットタグ

アーカイブ