cJc
cJc

Reputation: 863

How do I change the date format in a dataframe column.

I have a column in a DataFrame with dates in yyyymmdd format and I need to change it permanently to yyyy-mm-dd.

How can I do that?

Upvotes: 2

Views: 356

Answers (1)

DeepSpace
DeepSpace

Reputation: 81684

Given the info you provided in your comment, the column values can't be in the form yyyy-mm-dd since the column dtype is int64.

You can change the column dtype to be str, but the data won't be useful (ie you won't be able to do any date-calculations on it, though <, > should still work but lexicographically). If that's still what you wish and assuming df is the dataframe and the date column name is date:

def format_date_col(x):
    x = str(x)
    return '-'.join([x[:4], x[4:6], x[6:]])

    # or maybe like that for better readability:
    x = str(x)
    return '{year}-{month}-{day}'.format(year=x[:4], month=x[4:6], day=x[6:])

df['date'] = df['date'].apply(format_date_col)

A better approach would be to use actual date dtype:

from datetime import datetime 

def format_date_col(x):
    return datetime.strptime(str(x), '%Y%m%d')

df['date'] = df['date'].apply(format_date_col)

print df['date'].dtype
>> datetime64[ns]

Upvotes: 1

Related Questions