Fabio Pomi
Fabio Pomi

Reputation: 317

py pd DataFrame Timestamp to string conversion error (ValueError: cannot set a Timestamp with a non-timestamp str)

Last week the below code worked well to convert timestamp to string within a DataFrame:

df.at[i, 'VB12.GMA_DOC']
Timestamp('2022-01-12 00:00:00')

len_df = len(df.index)
df['GMA_DOC'] = ''
for i in range(0,len_df):
    df.at[i, 'VB12.GMA_DOC'] = df.at[i, 'VB12.GMA_DOC'].strftime('%Y-%m-%d')

Today, no changes to libraries or other parts of the code, I have the error:

ValueError: cannot set a Timestamp with a non-timestamp str

I noticed that directly from the shell there is no problem:

df.at[i, 'VB12.GMA_DOC'].strftime('%Y-%m-%d')
'2022-01-12'

After some tentatives I solved modifying the code as below:

len_df = len(df.index)
df['GMA_DOC'] = ''
for i in range(0,len_df):
    df.at[i, 'GMA_DOC'] = df.at[i, 'VB12.GMA_DOC'].strftime('%Y-%m-%d')
del  df['VB12.GMA_DOC']
df['VB12.GMA_DOC'] = df['GMA_DOC']
del  df['GMA_DOC']

The problem apparently is the direct assignment of the df_string to the previous df_timestamp column.

Is that normal or do you see a better solution to avoid the error ?

Upvotes: 0

Views: 297

Answers (1)

JANO
JANO

Reputation: 3066

I think the problem is that the type of your column is a Timestamp and you try to add a string to it. pandas tries to convert the string to a Timestamp, but it is unable to do so. In order to change both the values and the data type in one go, I'd recommend using a vectorized solution:

import pandas as pd

# Create dataframe with the timestamp values
df = pd.DataFrame(data=[{'VB12.GMA_DOC':'2022-01-12 00:00:01'}, {'VB12.GMA_DOC':'2022-01-11 00:00:00'}])
df['VB12.GMA_DOC'] = pd.to_datetime(df['VB12.GMA_DOC'], format="%Y-%m-%d %H:%M:%S")
print(df.dtypes) # datetime64[ns]


# Change timestamps to str
df['VB12.GMA_DOC'] = df['VB12.GMA_DOC'].dt.strftime('%Y-%m-%d')
print(df.dtypes) # object

df

Output:

VB12.GMA_DOC
0   2022-01-12
1   2022-01-11

Upvotes: 1

Related Questions