Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas illed:(我想我可以确定其他nan的位置,然后再将它们设置为nan(有更好的主意吗?)(请参见有问题的编辑)否则:是否有一种矢量化方法来实现此get_last_valid(…)? A1 _Pandas_Dataframe_Date_Datetime_Interpolation - Fatal编程技术网

Pandas illed:(我想我可以确定其他nan的位置,然后再将它们设置为nan(有更好的主意吗?)(请参见有问题的编辑)否则:是否有一种矢量化方法来实现此get_last_valid(…)? A1

Pandas illed:(我想我可以确定其他nan的位置,然后再将它们设置为nan(有更好的主意吗?)(请参见有问题的编辑)否则:是否有一种矢量化方法来实现此get_last_valid(…)? A1 ,pandas,dataframe,date,datetime,interpolation,Pandas,Dataframe,Date,Datetime,Interpolation,illed:(我想我可以确定其他nan的位置,然后再将它们设置为nan(有更好的主意吗?)(请参见有问题的编辑)否则:是否有一种矢量化方法来实现此get_last_valid(…)? A1 2019-06-17 00:00:00 NaN 2019-06-17 00:00:01 1.20 2019-06-17 00:01:59 1.00 2019-06-17

illed:(我想我可以确定其他nan的位置,然后再将它们设置为nan(有更好的主意吗?)(请参见有问题的编辑)否则:是否有一种矢量化方法来实现此get_last_valid(…)?
                              A1      
2019-06-17 00:00:00          NaN
2019-06-17 00:00:01         1.20      
2019-06-17 00:01:59         1.00    
2019-06-17 00:02:29          NaN        
                         ...        
2020-06-17 23:55:01          NaN     
2020-06-17 23:58:45         1.99  
2020-06-17 23:59:59          NaN  

specific_ indices  = 
DatetimeIndex([                   
'2019-06-17 00:00:01'  ,
'2019-06-17 00:01:59'  ,
'2019-06-17 00:02:29'  ,
'2020-06-17 23:55:01'  ,
'2020-06-17 23:58:45'  ,
'2020-06-17 23:59:59'  ]
                              A1  
2019-06-17 00:00:00          NaN     
2019-06-17 00:00:01         1.20      
2019-06-17 00:01:59         1.00    
2019-06-17 00:02:29         1.00       
                         ...        
2020-06-17 23:55:01         1.00     
2020-06-17 23:58:45         1.99  
2020-06-17 23:59:59         1.99 

# when I use MrFuppes Function:
def get_last_valid(df, t, colname):
    try:
        i = np.flatnonzero(df.index == t)[0]
    except IndexError:
        return np.nan
    else:
        return df[colname].iloc[:i+1].dropna().iloc[-1]

# with this specified index to fill the NaNs of:
dti=  pd.to_datetime( pd.Index(['2019-06-17 00:02:29', '2020-06-17 23:59:59'], dtype='datetime64[ns]', freq=None) ) 

# then also the indices between the last valid and the NaN to fill
# are filled by the valid value. 
# I could save the location of the other NaNs not to fill and 
#later set them NaN again (or is there a better idea?)
                       A1
2019-06-17 00:00:00   NaN
2019-06-17 00:00:01  1.20
2019-06-17 00:01:59  1.01 # <== should be NaN
2019-06-17 00:02:29  1.01
2020-06-17 23:55:01   NaN
2020-06-17 23:58:45  1.99
2020-06-17 23:59:59  1.99

import pandas as pd
import numpy as np

# df
#                        A1
# 2019-06-17 00:00:00   NaN
# 2019-06-17 00:00:01  1.20
# 2019-06-17 00:01:59  1.00
# 2019-06-17 00:02:29   NaN
# 2020-06-17 23:55:01   NaN
# 2020-06-17 23:58:45  1.99
# 2020-06-17 23:59:59   NaN

# dti
# DatetimeIndex(['2019-06-17 00:01:59', '2020-06-17 23:59:59'], dtype='datetime64[ns]', freq=None)

def get_last_valid(df, t, colname):
    try:
        i = np.flatnonzero(df.index == t)[0]
    except IndexError:
        return np.nan
    else:
        return df[colname].iloc[:i+1].dropna().iloc[-1]
    
for t in dti:
    print(t.isoformat(), get_last_valid(df, t, 'A1'))

# 2019-06-17T00:01:59 1.0
# 2020-06-17T23:59:59 1.99
for t in dti:
    df.loc[df.index == t, 'A1'] = df['A1'].iloc[:(df.index == t).nonzero()[0][0]+1].dropna().iloc[-1]