-1

I am trying to get results for specific attributes? Trying to extract month and day of the week from Start Time to create new columns then filer to get new data frame

import pandas as pd
    
CITY_DATA = {'chicago': 'chicago.csv',
             'new york city': 'new_york_city.csv','washington': 'washington.csv'}


def load_data(city, month, day):
    df = pd.read_csv(CITY_DATA[city])

    df['Start Time'] = pd.to_datetime(df['Start Time'])

    df['month'] = df['Start Time'].dt.month
    df['day_of_week'] = df['Start Time'].dt.weekday_name


    if month != 'all':
        months = ['january', 'february', 'march', 'april', 'may', 'june']
        month = months.index(month) + 1


        df = df[df['month'] == month]


    if day != 'all':
        df = df[df['day_of_week'] == day.title()]

    return df


df = load_data('chicago', 'march', 'friday')
print(df.head())

AttributeError: 'DatetimeProperties' object has no attribute 'weekday_name'

Roman Zh.
  • 985
  • 2
  • 6
  • 20
Pola Ayaad
  • 11
  • 3
  • your question is mostly code, explain what are you trying to do as well – stuckoverflow Jan 18 '21 at 16:45
  • A pandas timestamp does not have an attribute titled 'weekday_name', it does however have an attribute titled 'day_name'. In would try chnaging the line ```df['day_of_week'] = df['Start Time'].dt.weekday_name``` to ```df['day_of_week'] = df['Start Time'].dt.day_name``` – itprorh66 Jan 18 '21 at 16:47
  • possible duplicate of https://stackoverflow.com/questions/60214194/error-in-reading-stock-data-datetimeproperties-object-has-no-attribute-week – stuckoverflow Jan 18 '21 at 16:49

1 Answers1

3

Your problem is the following line:

df['day_of_week'] = df['Start Time'].dt.weekday_name

Change it to:

df['day_of_week'] = df['Start Time'].dt.day_name()
Shag
  • 430
  • 2
  • 5