As of version 17.0, you can format with the dt accessor:
df['DateStr'] = df['DateObj'].dt.strftime('%d%m%Y')
Answer from Kamil Sindi on Stack OverflowAs of version 17.0, you can format with the dt accessor:
df['DateStr'] = df['DateObj'].dt.strftime('%d%m%Y')
In [6]: df = DataFrame(dict(A = date_range('20130101',periods=10)))
In [7]: df
Out[7]:
A
0 2013-01-01 00:00:00
1 2013-01-02 00:00:00
2 2013-01-03 00:00:00
3 2013-01-04 00:00:00
4 2013-01-05 00:00:00
5 2013-01-06 00:00:00
6 2013-01-07 00:00:00
7 2013-01-08 00:00:00
8 2013-01-09 00:00:00
9 2013-01-10 00:00:00
In [8]: df['A'].apply(lambda x: x.strftime('%d%m%Y'))
Out[8]:
0 01012013
1 02012013
2 03012013
3 04012013
4 05012013
5 06012013
6 07012013
7 08012013
8 09012013
9 10012013
Name: A, dtype: object
error: "str" has no attribute "strftime"
How i can resolve error "AttributeError: 'str' object has no attribute 'strftime'" and "TranslateModel object has no member named..."
'Str' object has no attribute 'strftime'
What do you want us to say that the error message doesn't? You have a variable called time, you are attempting to call a method called strftime on it, but because it's a str, it doesn't have a strftime attribute. So look at where you're defining time, realise that you're assigning a string to it instead of a datetime, and fix that.
python - AttributeError: 'str' object has no attribute 'strftime' when modifying pandas dataframe - Stack Overflow
Hello folks! I have a cuestion related with:
error: "str" has no attribute "strftime"
The error is right here:
if query_params is not None:
query_params = query_params.replace("{{cinema_id}}", cinema_vista_id)
query_params = query_params.replace( "{{start_date}}", start_date.strftime("%Y-
%m-%d") ) query_params = query_params.replace( "{{end_date}}", end_date.strftime("%Y-%m-%d") )
I found a page which mentioned this https://bobbyhadz.com/blog/python-attributeerror-str-object-has-no-attribute-strftime#:~:text=The%20Python%20%22AttributeError%3A%20'str,to%20one%20before%20calling%20strftime%20.
I dont know how to solve this or how to adapt it, can you help me?
I don't have access to my computer right now so I have been coding on mobile lately. I was making a script when I ran into this error after inputting the "clear" command:
Traceback (most recent call last): File "/storage/emulated/0/qpython/timetest.py", line 19, in <module> console() File "/storage/emulated/0/qpython/timetest.py", line 17, in console console() File "/storage/emulated/0/qpython/timetest.py", line 14, in console timedisplay() File "/storage/emulated/0/qpython/timetest.py", line 8, in timedisplay date = time.strftime("%a, %b %d, %Y") AttributeError: 'str' object has no attribute 'strftime'
This was part of a bigger script that I cannot post but I recreated part of it and still ran into the same error:
import datetime import time import os
def timedisplay(): global date global time date = time.strftime("%a, %b %d, %Y") time = time.strftime("%I:%M %p") print(date) print(time)
def console(): timedisplay() test = raw_input("") os.system(test) console()
console()
It displayed fine until I cleared the screen using os.system.
What do you want us to say that the error message doesn't? You have a variable called time, you are attempting to call a method called strftime on it, but because it's a str, it doesn't have a strftime attribute. So look at where you're defining time, realise that you're assigning a string to it instead of a datetime, and fix that.
you need to import datetime like this
from datetime import datetime