Python Forum

Full Version: HELP- DATA FRAME INTO TIME SERIES- BASIC
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ok, so i have a data frame and i want to change it to time series.

Output:
Month Sales Inflation 0 1-Jan 266.0 1 1 2-Jan 145.9 3 2 3-Jan 183.1 5 3 4-Jan 119.3 5 4 5-Jan 180.3 4
For some reason,below code did not work.(also if anyone can explain what it means it would help a lot i just found the code from tutorial, didn't really get it)

shampoo= pd.read_csv('shampoo_with_exog.csv', index_col=[0], parse_dates= True,squeeze=True)
Since it didn't work, i am wanted to do it manually. What i want to do is only to keep the Jan,Feb,... and strip up until including '-' for all in Months column

def month_converter(month):
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    return months.index(month) + 1

### this is the code for the striping part which i don't know how to do!!! Then,

for i in shampoo.Month :
    month_converter(i)
    

shampoo_index = shampoo['Month'].str.index('-')

shampoo.Month = shampoo['Month'].str[(shampoo_index+1):]

for i in shampoo.Month :
    month_converter(i)
Why doesn't this work?

SOMEONE PLEASE HELP!

Error:
Can only use .str accessor with string values, which use np.object_ dtype in pandas