Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas 0.15 DataFrame: Remove or reset time portion of a datetime64

Tags:

python

pandas

I have imported a CSV file into a pandas DataFrame and have a datetime64 column with values such as:

2014-06-30 21:50:00

I simply want to either remove the time or set the time to midnight:

2014-06-30 00:00:00 

What is the easiest way of doing this?

like image 860
n4cer500 Avatar asked Oct 23 '14 15:10

n4cer500


1 Answers

Pandas has a builtin function pd.datetools.normalize_date for that purpose:

df['date_col'] = df['date_col'].apply(pd.datetools.normalize_date)

It's implemented in Cython and does the following:

if PyDateTime_Check(dt):
    return dt.replace(hour=0, minute=0, second=0, microsecond=0)
elif PyDate_Check(dt):
    return datetime(dt.year, dt.month, dt.day)
else:
    raise TypeError('Unrecognized type: %s' % type(dt))
like image 64
Frank Avatar answered Oct 19 '22 06:10

Frank