我有以下数据框:
public static boolean isAvailable(Context context)
{
GoogleApiAvailability availability = GoogleApiAvailability.getInstance();
return isGooglePlayServicesAvailable(context, availability) &&
isCastContextAvailable(context);
}
public static boolean isAvailable(Context context) {
if (googlePlayServicesVerified(context)) {
try {
castContext = CastContext.getSharedInstance(context);
Log.d(TAG, "CastContext instantiated");
} catch (Exception e) {
Log.report(e);
castContext = null;
}
} else {
CrashReporter.report("CastContext FAILED to be instantiated : googlePlayServicesVerified() has failed."));
castContext = null;
}
}
我想用time = 2填充时间戳中的所有空白,因此输出将如下:
timestamp value
2018-02-26 09:13:00 0.972198
2018-02-26 09:14:00 1.008504
2018-02-26 09:15:00 1.011961
2018-02-26 09:18:00 1.018950
2018-02-26 09:19:00 1.008538
2018-02-26 09:21:00 0.988535
2018-02-26 09:22:00 0.944170
2018-02-26 09:23:00 0.940284
我使用以下代码首先填补时间戳中的空白:
timestamp value
2018-02-26 09:13:00 0.972198
2018-02-26 09:14:00 1.008504
2018-02-26 09:15:00 1.011961
2018-02-26 09:16:00 2.0
2018-02-26 09:17:00 2.0
2018-02-26 09:18:00 1.018950
2018-02-26 09:19:00 1.008538
2018-02-26 09:20:00 2.0
2018-02-26 09:21:00 0.988535
2018-02-26 09:22:00 0.944170
2018-02-26 09:23:00 0.940284
但出现以下错误。我想知道我在这里想念的是什么?谢谢!
df.reindex(index = 'timestamp')
答案 0 :(得分:4)
使用resample
+ fillna
df.set_index('timestamp').resample('60s').mean().fillna(2).reset_index()
Out[907]:
timestamp value
0 2018-02-26 09:13:00 0.972198
1 2018-02-26 09:14:00 1.008504
2 2018-02-26 09:15:00 1.011961
3 2018-02-26 09:16:00 2.000000
4 2018-02-26 09:17:00 2.000000
5 2018-02-26 09:18:00 1.018950
6 2018-02-26 09:19:00 1.008538
7 2018-02-26 09:20:00 2.000000
8 2018-02-26 09:21:00 0.988535
9 2018-02-26 09:22:00 0.944170
10 2018-02-26 09:23:00 0.940284
答案 1 :(得分:2)
date_range
ts = pd.date_range(df.timestamp.min(), df.timestamp.max(), freq='1min')
set_index
, reindex
和 fillna
df.set_index('timestamp').reindex(ts).fillna(2.0).rename_axis('timestamp').reset_index()
timestamp value
0 2018-02-26 09:13:00 0.972198
1 2018-02-26 09:14:00 1.008504
2 2018-02-26 09:15:00 1.011961
3 2018-02-26 09:16:00 2.000000
4 2018-02-26 09:17:00 2.000000
5 2018-02-26 09:18:00 1.018950
6 2018-02-26 09:19:00 1.008538
7 2018-02-26 09:20:00 2.000000
8 2018-02-26 09:21:00 0.988535
9 2018-02-26 09:22:00 0.944170
10 2018-02-26 09:23:00 0.940284