Skip to content
Advertisement

Add additional timestamp to Pandas DataFrame items based on item timestamp/index

I have a large time-indexed Pandas DataFrame with time-series data of a couple of devices. The structure of this DataFrame (in code below self._combined_data_frame) looks like this:

JavaScript

The DateTimeIndex and device_name are filled for every row, the other columns contain nan values. Sample data is available on Google Drive:

Sample data set

Then there is a list with reference timestamps from another source (in code below self._lastinjection_items). What I would like to do is to add to each item in the DataFrame a reference timestamp such that to each timestamped item the “next smaller” reference timestamp is added. Pseudocode:

JavaScript

Currently I tried to use the DataFrame.apply(lambda item: ...) function, but I can’t even figure out how to access the timestamp of the individual items.

What would be the most Pythonic way to realize what I want?

Current code looks like this:

JavaScript

EDIT:

JavaScript

Expected result:

JavaScript

In real life: enter image description here

Advertisement

Answer

IIUC use merge_asof:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement