Jaffer Wilson
Jaffer Wilson

Reputation: 7273

Remove rows where datetime is a specific day between specific times

I am using this dataset:

https://gist.github.com/JafferWilson/2c468679fa66c04c08a0ca624ca92d8f

What I want to achieve is:

  1. Check the day is Monday or Friday
  2. If so, remove the first 2 hours values from Monday and the last 2 hours values from Friday from the Dataframe in pandas

I have tried to load the values from the csv using the pandas dataframe as:

import pandas as pd
df = pd.read_csv("EURM1.csv")

But I don't know how to sort the data in the fashion I want. I tried using the startdate and enddate, but they are of no use to me.

Please help me to get the data in the form I want it.

Upvotes: 1

Views: 97

Answers (1)

iacob
iacob

Reputation: 24201

Once you have converted the column to datetime, you can easily perform a conditional operation on it as per your logic:

df["date"] = pd.to_datetime(df["date"], format="%Y.%m.%d %H:%M:%S")

df = df.loc[~((df["date"].dt.weekday_name == "Monday") # Excludes Mondays before 2am
            & (df["date"].dt.hour < 2)) 

          & ~((df["date"].dt.weekday_name == "Friday") # Excludes Fridays after 10pm
            & (df["date"].dt.hour >= 22))]

Upvotes: 3

Related Questions