59

I'm trying to convert a datetime column back to a string in Pandas dataframe.

the syntax I have so far is:

all_data['Order Day new'] = dt.date.strftime(all_data['Order Day new'], '%d/%m/%Y')

but this returns the error:

descriptor 'strftime' requires a 'datetime.date' object but received a 'Series'.

Can anyone tell me where I'm going wrong.

4 Answers 4

102

If you're using version 0.17.0 or higher then you can call this using .dt.strftime which is vectorised:

all_data['Order Day new'] = all_data['Order Day new'].dt.strftime('%Y-%m-%d')

** If your pandas version is older than 0.17.0 then you have to call apply and pass the data to strftime:

In [111]:

all_data = pd.DataFrame({'Order Day new':[dt.datetime(2014,5,9), dt.datetime(2012,6,19)]})
print(all_data)
all_data.info()
  Order Day new
0    2014-05-09
1    2012-06-19
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2 entries, 0 to 1
Data columns (total 1 columns):
Order Day new    2 non-null datetime64[ns]
dtypes: datetime64[ns](1)
memory usage: 32.0 bytes

In [108]:

all_data['Order Day new'] = all_data['Order Day new'].apply(lambda x: dt.datetime.strftime(x, '%Y-%m-%d'))
all_data
Out[108]:
  Order Day new
0    2014-05-09
1    2012-06-19
In [109]:

all_data.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2 entries, 0 to 1
Data columns (total 1 columns):
Order Day new    2 non-null object
dtypes: object(1)
memory usage: 32.0+ bytes

You can't call strftime on the column as it doesn't understand Series as a param hence the error

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

1 Comment

In pandas 1.0.5 this is producing a SettingWithCopyWarning.
37
all_data['Order Day new']=all_data['Order Day new'].astype(str)

I think this is more simple, if the date is already in the format you want it in string form.

7 Comments

Please add some explanation to your answer by editing it, such that others can learn from it
@NicoHaase not much to explain I think, just using the .astype method
Well, then explain why you've used it anyways. The accepted answer (that means: the one that solved the OP's problem) does not use it, and then you should explain what makes your solution better
I checked it and yes
I like this answer, and it seems to work with NaT values in the column.
|
17

For converting all datetime columns to string use the below code.

for x in  df.select_dtypes(include=['datetime64']).columns.tolist():
    df[x] = df[x].astype(str)

or

date_columns = df.select_dtypes(include=['datetime64']).columns.tolist()
df[date_columns] = df[date_columns].astype(str)

Comments

0

In my case I had a pySpark dataframe. This is how I converted "day" of type timestamp, to a string:

import pyspark.sql.functions as F
convertedDf = rawDf.withColumn('d2', F.date_format(rawDf['day'], 'yyyyMMdd'))

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.