Being all new to pandas, I have a probably very basic question. I have a camera in a tawny owl nest box and I noted down the time the owl spends in the nestbox. I want to visualize the time in the nestbox using Python. I made a pandas DatetimeIndex, and used it to make a pandas dataframe as follows. However, this shows the owl being present all the time. Is there a way to plot a 1 only in the time ranges specified by the DatetimeIndex?
import pandas as pd
import matplotlib.pyplot as plt
presence = pd.DatetimeIndex(["2021-12-01 18:08","2021-12-01 18:11",
"2021-12-02 05:27","2021-12-02 05:29",
"2021-12-02 22:40","2021-12-02 22:43",
"2021-12-03 19:24","2021-12-03 19:27",
"2021-12-06 18:04","2021-12-06 18:06",
"2021-12-07 05:28","2021-12-07 05:30",
"2021-12-10 03:05","2021-12-10 03:10",
"2021-12-10 07:11","2021-12-10 07:13",
"2021-12-10 20:40","2021-12-10 20:41",
"2021-12-12 19:42","2021-12-12 19:45",
"2021-12-13 04:13","2021-12-13 04:17",
"2021-12-15 04:28","2021-12-15 04:30",
"2021-12-15 05:21","2021-12-15 05:25",
"2021-12-15 17:40","2021-12-15 17:44",
"2021-12-15 22:31","2021-12-15 22:37",
"2021-12-16 04:24","2021-12-16 04:28",
"2021-12-16 19:58","2021-12-16 20:09",
"2021-12-17 17:42","2021-12-17 18:04",
"2021-12-17 22:19","2021-12-17 22:26",
"2021-12-18 05:41","2021-12-18 05:44",
"2021-12-19 07:40","2021-12-19 16:55",
"2021-12-19 20:39","2021-12-19 20:52",
"2021-12-19 21:56","2021-12-19 23:17",
"2021-12-21 04:53","2021-12-21 04:59",
"2021-12-21 05:37","2021-12-21 05:39",
"2021-12-22 08:06","2021-12-22 18:00",
])
df = pd.DataFrame({'owl presence' : np.ones(len(presence))}, index=presence)
df.plot()
``
If your presence values are:
start observation, end observation, start observation, end observation, etc.
You might want to generate a timeframe with every minute, where value = 1
if owl is present, value = 0
if owl is not present.
You can do this by creating a dataframe as such:
presence_df=pd.DataFrame(data={'value':[1,0]*int(len(presence)/2)}, index=presence)
df = pd.DataFrame(index=pd.date_range(presence.min(), presence.max(), freq='min'))
df2 = df.merge(presence_df, left_index=True, right_index=True, how='left').ffill()
df2.plot()
df2
would look something like:
value
2021-12-01 18:08:00 1.0
2021-12-01 18:09:00 1.0
2021-12-01 18:10:00 1.0
2021-12-01 18:11:00 0.0
2021-12-01 18:12:00 0.0
... ...
2021-12-22 17:56:00 1.0
2021-12-22 17:57:00 1.0
2021-12-22 17:58:00 1.0
2021-12-22 17:59:00 1.0
2021-12-22 18:00:00 0.0
df2.plot()
would look like this:
Of course this needs some customization to make it pretty ;)