仅回填最后N个项目

时间:2018-11-23 14:24:33

标签: pandas

我有这个简单的时间序列

In [1]: df = pd.DataFrame({'fire': [1, 1, 1]}, 
   ...:                   index=pd.to_datetime([
   ...:                       '2016-03-16 23:20:10', 
   ...:                       '2016-03-16 23:28:58', 
   ...:                       '2016-03-16 23:38:15']))
   ...:                   

In [2]: df
Out[2]: 
                     fire
2016-03-16 23:20:10     1
2016-03-16 23:28:58     1
2016-03-16 23:41:15     1

我想在1分钟前对其进行降采样,然后添加另一个名为 fire_in_the_next_5_minutes 的列。重采样很容易完成,但是我找不到将回填限制为仅前5行的方法。我得到的最接近的数据是:

In [3]: df = df.resample('1min').mean()
   ...: df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill')
   ...: 

In [4]: df
Out[4]: 
                     fire  fire_in_the_next_5_minutes
2016-03-16 23:20:00   1.0                         1.0
2016-03-16 23:21:00   NaN                         1.0  <-- should remain NaN
2016-03-16 23:22:00   NaN                         1.0  <-- should remain NaN
2016-03-16 23:23:00   NaN                         1.0
2016-03-16 23:24:00   NaN                         1.0
2016-03-16 23:25:00   NaN                         1.0
2016-03-16 23:26:00   NaN                         1.0
2016-03-16 23:27:00   NaN                         1.0
2016-03-16 23:28:00   1.0                         1.0
2016-03-16 23:29:00   NaN                         1.0  <-- should remain NaN
2016-03-16 23:30:00   NaN                         1.0  <-- should remain NaN
2016-03-16 23:31:00   NaN                         1.0  <-- should remain NaN
2016-03-16 23:32:00   NaN                         1.0  <-- should remain NaN
2016-03-16 23:33:00   NaN                         1.0
2016-03-16 23:34:00   NaN                         1.0
2016-03-16 23:35:00   NaN                         1.0
2016-03-16 23:36:00   NaN                         1.0
2016-03-16 23:37:00   NaN                         1.0
2016-03-16 23:38:00   1.0                         1.0

是否可以通过其他方式回填,而不是使用fillna方法?

2 个答案:

答案 0 :(得分:2)

public class MainViewModel extends AndroidViewModel implements RecyclerViewAdapter.OnItemClickListener { private TaskRepository taskRepository; private LiveData<List<Task>> allTasks; public final ObservableField<String> description = new ObservableField<>(); public final ObservableField<String> date = new ObservableField<>(); public final ObservableField<String> time = new ObservableField<>(); private String TAG; private int ID; private Task mTask; public MainViewModel(@NonNull Application application) { super(application); taskRepository = new TaskRepository(application); allTasks = taskRepository.getAllTasks(); } public LiveData<List<Task>> getAllTasks() { return allTasks; } public void deleteTask(Task task) { taskRepository.delete(task); } public void getDataToEdit() { editTask(description.get(), date.get(), time.get()); } public void editTask(String description, String date, String time) { Task task = new Task(description, date, time); task.setId(ID); taskRepository.update(task); Log.d(TAG, "MVM editTask " + task.getId()); } @Override public void onItemClick(View view, Task item) { Log.d(TAG, "MVM " + item.getDescription() + " / " + item.getDate() + " / " + item.getTime() + " / " + mTask); description.set(item.getDescription()); date.set(item.getDate()); time.set(item.getTime()); ID = item.getId(); mTask.setId(item.getId()); if (mTask != null) { mTask.setId(item.getId()); } } bfill一起使用

limit

答案 1 :(得分:0)

您需要为limit提供fillna参数:

df['fire_in_the_next_5_minutes'] = df['fire'].fillna(method='backfill', limit=5)