-1

This is a follow up question (?) to a post I made yesterday: Python pandas df.loc not being found in a table

I'm trying to I'm trying to find a certain row and column in a given .csv file with pandas. Here is a snapshot of the table: Table

The table goes from 1/1/2015 to 12/31/2017. I've located the specific column and date I want to use from the csv file, and printing it so I can see if it's working properly. This is the code I have thus far:

months = {'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6,
      'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12}
month = str(input('Enter a month: '))
year = str(input('Enter a year: '))
if not (2015 <= float(year) <= 2017):
    print('Enter a year bewteen 2015 and 2017, both included and try again')
    exit()

day = 1
df1 = df.set_index(['Date'])

if (month == 'January' or 'March' or 'May' or 'July' or 'August' or 'October' or 'December'):
    while day < 32:
        find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
        print(find)
        day += 1
elif (month == 'April' or 'June' or 'September' or 'November'):
    while day < 31:
        find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
        print(find)
        day += 1
elif (month == 'February'):
    if year == '2016':
        while day < 29:
            find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
            print(find)
            day += 1
    else:
        while day < 28:
            find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
            print(find)
            day += 1

This code is working correctly for months with 31 days, but breaks for any other month. For example if I enter "June" for the month (no quotations), the code works until it reaches day 30, then tries to look for day 31. Why is this happening? It seems to be searching for day 31 in the date column even though I've restricted it to be less than 31.

Picture of error code: Error

Astro
  • 1
  • 2
  • Please provide the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. You posted over 30 lines of text for a 3-line problem, and didn't trace your execution. – Prune Oct 24 '20 at 20:03

2 Answers2

0

Your or statements are plain wrong.

if (month == 'January' or 'March' or 'May' or 'July' or 'August' or 'October' or 'December'):

always evaluates to true; you'll want to replace that idiom with e.g.

if month in ('January', 'March', 'May', 'July', 'August', 'October', 'December'):
AKX
  • 152,115
  • 15
  • 115
  • 172
0

It's as simple as changing your if and elif statements.

if (month == 'January' or 'March' or 'May' or 'July' or 'August' or 'October' or 'December'):

think of the or statement as another if: you have to compare it to the month again. In other words: in your if statement, you're literally saying "if month is equal to January or if march or if May or if July ..." As we know, If 'July' really doesn't mean anything.

try : if month in ('January', 'March', 'May', 'July', 'August', 'October', 'December'):