1

I want to choose certain days from a dataframe. I have tried this code. It seems to work, but when I select a longer period of days, for example from 10.03.2020 to 20.03.2020, it returns only days from 13 to 20. I want to see the values from the dataframe coresponding to days 10->20. I cannot find the problem...

import pandas as pd


def select_needed_dates(month_start, day_start, year_start, month_end, day_end, year_end):
    data = pd.read_csv("https://raw.githubusercontent.com/iulianastroia/csv_data/master/oct-march.csv")
    data['day'] = pd.to_datetime(data['day'])
    data = data.loc[(data['day'] >= year_start + "-" + day_start + "-" + month_start) & (
            data['day'] <= year_end + "-" + day_end + "-" + month_end)]
    print(data)

select_needed_dates("10", "03", "2020", "20", "03", "2020")


Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
moro_922
  • 113
  • 8

1 Answers1

1

You need to specify the strftime format to parse the time in this case it should be %d/%m/%Y.

Replace:

data['day'] = pd.to_datetime(data['day'])

With:

data['day'] = pd.to_datetime(data['day'], format="%d/%m/%Y")
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53