1

In python I have a data set with these columns:

ID_Order    ID_Customer ID_Item DateTime_CartFinalize   Amount_Gross_Order  city_name_fa    Quantity_item

It is for more than 200000 transactions. I am going to use association rules. At the first step, I have to Change the DateTime_CartFinalize column to timestamp format using pd.Timestamp function. Before that, ensure that the date format in your digiasso.csv file is in this 2/11/18 12:29 AM format. Hint: use the aggregate function. ???

1 Answer 1

1

I assume that you have a DateFrame with DateTime_CartFinalize column of String type (actually object).

Then you can convert this column calling:

df.DateTime_CartFinalize = pd.to_datetime(df.DateTime_CartFinalize)

pd.to_datetime is clever enough to recognize the actual format of source data. Or you can use dayfirst, yearfirst or format parameters to pass a hint how to convert the source string. Read the documentation of this function.

Yet another option: If you read your DataFrame with e.g. read_csv, then use parse_dates parameter, passing indices or names of columns which should be converted to dates, as early as at the reading step.

Sign up to request clarification or add additional context in comments.

5 Comments

At the first glance it looks OK. Try and check the result.
df.DateTime_CartFinalize=df.DateTime_CartFinalize.agg(pd.Timestamp) , is it corret too?
I want to add a new column named year and put the year of each transaction in it. I want to use the “lambda” to define a function and apply it to all elements using the “agg” function. I wrote this, but I got an error: df.yearr=df.DateTime_CartFinalize.agg(lambda x: x.year)
I know another way with interval : >>> def interval(x): interval=(x.year) return(interval) >>> df['year']=df['DateTime_CartFinalize'].agg(interval)
I tried your code and got no error, but I started with df['yearr'] = ... (maybe the source of your error is that yearr column does not exist yet, so you can not use the "dot notation"). On the other hand, agg is used rather with groupy. A more intuitive solution is e.g. df['yearr'] = df.DateTime_CartFinalize.dt.year.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.