0

I have a data frame with multiple columns including a datetime column called timestamp, which contains date + time. I would like to filter all rows for a specific date, i.e. all times on that date. See first couple of rows in table below, continues until 2022-12-31 23:00:00.

Index timestamp date week price costs_per_hour
0 2022-07-01 00:00:00 2022-07-01 26 268.71 300
1 2022-07-01 01:00:00 2022-07-01 26 267.64 400
2 2022-07-01 02:00:00 2022-07-01 26 270.18 500

Desired output is the table above, ending at 2022-07-01 23:00:00.

I tried multiple ways:

    filtered_df = df_def_costs.loc[(df_def_costs['timestamp'] == '2022-07-01')]

which only gives

       timestamp        date  week  usage   price  costs_per_hour
0 2022-07-01  2022-07-01    26  449.4  268.71      300

and omits the other entries on 2022-07-01.

Alternative

    df_def_costs[df_def_costs['timestamp'].dt.date==dt.datetime.strptime('2022-07-01', '%Y-%m-%d')]

results in empty dataframe

  • You can `normalize` the dates: `df_def_costs.loc[pd.to_datetime(df_def_costs['timestamp']).dt.normalize().eq('2022-07-01')]` – mozway Jan 20 '23 at 14:28

0 Answers0