Reputation: 63
I try to Generate Monthly KPIs DataFrame by aggregating Revenue but i got this error :
AttributeError: 'DatetimeIndex' object has no attribute 'Year_Lease_Start'
Here is the format of the dataframe :
VEHICLE_TYPE MARQUE COMPANY_TYPE COMPANY_NAME COMPANY_CODE PERSONAL SALESCHANNEL_SUB_CODE COMP_SEG_SUB_CODE BELONGS_UNDER_HIERARCHY_CODE DESCRIPTION CREATED_BY DATE_CREATED ACCESS_SET LEASE_ID TERM DISTANCE LEASE_START LEASE_END LEASE_EXTENDED_TO STATUS QUOTE_ID QUOTE_REVISION TITLE_NAME LOYER_FINANCIER_HT LOYER_FINANCIER_TTC PRESTATION_HT PRESTATION_TTC PRIX LOYER_TOTAL_HT Year_Lease_Start Month_Lease_Start YearMonth_Lease_Start
Date
2018-09-05 NI rgrgCUS lhkv erge 506183 Y DIRECT EPE 002053679_HR YHE 179868.0 2019-01-23 13:27:39.0 AG_PRime 385590 36 45000 2018-09-05 2021-09-04 00:00:00.0 NaN ACTIVE 3269111 1 Adooper 217.42 260.904 42.57 51.084 35500.0 259.99 2018 9 20180
Any idea please to fix this problem? thanks
%pylab inline
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="white", color_codes=True)
# Transforming Lease_start_Date column to datetime type and mapping to a new columne as ILease_start_Date
DIRECT_PART_df.LEASE_START= pd.to_datetime(DIRECT_PART_df.LEASE_START)
# Construct Year, Month and YearMonth from Lease Start Date field
DIRECT_PART_df['Year_Lease_Start'], DIRECT_PART_df['Month_Lease_Start'] = DIRECT_PART_df['LEASE_START'].dt.year, DIRECT_PART_df['LEASE_START'].dt.month
DIRECT_PART_df['YearMonth_Lease_Start'] = DIRECT_PART_df['LEASE_START'].map(lambda x: 100*x.year + x.month)
# Create "Date" column in datetime format to use for index
DIRECT_PART_df['Date'] = pd.to_datetime(DIRECT_PART_df.LEASE_START.dt.date)
DIRECT_PART_df.set_index('Date', inplace=True)
# 1. Revenue - Generate Monthly KPIs DataFrame by aggregating Revenue
m_kpis = pd.DataFrame(DIRECT_PART_df.groupby([DIRECT_PART_df.index.Year_Lease_Start, DIRECT_PART_df.index.Year_Lease_Start.Month_Lease_Start])['LOYER_TOTAL_HT'].sum())
Upvotes: 2
Views: 3817
Reputation: 862406
Problem is in last row, for columns names use strings, not DIRECT_PART_df.index.Year_Lease_Start
:
m_kpis = DIRECT_PART_df.groupby(['Year_Lease_Start', 'Month_Lease_Start'], as_index=False)['LOYER_TOTAL_HT'].sum()
If need also pass index:
m_kpis = DIRECT_PART_df.groupby([DIRECT_PART_df.index.year.rename('Year'), DIRECT_PART_df.index.month.rename('Month'), as_index=False)['LOYER_TOTAL_HT'].sum()
Upvotes: 1