0

So I have a really crappy method and want to improve it and just generally move away from using iterrows, as I understand it's just bad practice.

I basically want to check whether a value in the 'Login Date' column falls with a range of datetime objects.

This is my crappy method atm.

for index, row in df.iterrows():
if semester_start <= datetime.strptime(row['Login Date'].split(' ')[0], "%Y/%m/%d") <= semester_end:
    index_list.append(index)

filtered_df = df[df.index.isin(index_list)]
  • A _small_ subset of your data as a __copyable__ piece of code that can be used for testing as well as your expected output for the __provided__ data can really help to clarify the scope of the task this code is trying to accomplish and make the question more useful to future readers. See [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/15497888) for some examples. – Henry Ecker Mar 11 '22 at 17:29

1 Answers1

0

I think using pd.to_datetime and pd.Series.between should work for you:

filtered_df = df[pd.to_datetime(df['Login Date'].str.split(' ').str[0], format="%Y/%m/%d").between(semester_start, semester_end)]