0

I have datetime field like 2017-01-15T02:41:38.466Z and would like to convert it to %Y-%m-%d format. How can this be achieved in pandas or python?

I tried this

frame['datetime_ordered'] = pd.datetime(frame['datetime_ordered'], format='%Y-%m-%d')

but getting the error

cannot convert the series to <class 'int'>

4
  • 2
    Welcome to StackOverflow @NitinVarghese. Please refer How do I ask a good question? Commented Jan 21, 2021 at 8:54
  • try pd.to_datetime(frame['datetime_ordered'], format='%Y-%m-%d') Commented Jan 21, 2021 at 9:05
  • The following worked d_parser= lambda x: pd.datetime.strptime(x,'%Y-%m-%dT%H:%M:%S.%fZ') for filename in all_files: df = pd.read_csv(filename, index_col=None, header=0,parse_dates=['datetime_ordered'],date_parser=d_parser) li.append(df) frame = pd.concat(li, axis=0, ignore_index=True) Commented Jan 21, 2021 at 9:14
  • I suggest that you add it as an answer to your question. Commented Jan 21, 2021 at 9:38

2 Answers 2

2

The following code worked

d_parser= lambda x: pd.datetime.strptime(x,'%Y-%m-%dT%H:%M:%S.%fZ')
for filename in all_files: 
    df = pd.read_csv(filename, index_col=None, header=0,parse_dates['datetime_ordered'],date_parser=d_parser) 
    li.append(df) 
    frame =pd.concat(li, axis=0, ignore_index=True) 
Sign up to request clarification or add additional context in comments.

Comments

0
import datetime
from datetime import datetime
date_str="2017-01-15T02:41:38.466Z"
a_date=pd.to_datetime(date_str)
print("date time value", a_date)
#datetime to string with format
print(a_date.strftime('%Y-%m-%d'))

Comments

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.