Reputation: 6333
I have multiple columns in the format yyyy-mm-dd hh:mm:ss
and I want to transform all of them to yyyy-mm-dd
format with a single (preferably vectorized) line of code.
In R, I would do something along the lines of:
df[, c("col1", "col2") := list(col1 = as.Date(col1), col2 = as.Date(col2))]
I want to avoid doing this individually for every column. So far I can only think of the following trivial solution:
df["col1"] = df["col1"].dt.date
df["col2"] = df["col2"].dt.date
So I have two questions: how do you operate on multiple columns in a single line of code and how do you turn datetime
objects to date
.
Upvotes: 2
Views: 2901
Reputation: 25249
You may use select_dtypes
to select only datetime
dtype columns and stack
, convert to dt.date
and unstack
and update back to df
df.update(df.select_dtypes('datetime').stack().dt.date.unstack())
Upvotes: 2
Reputation: 3910
# sample data
df = pd.DataFrame({
'a': np.random.choice(pd.date_range("2019-01-01", "2019-12-31", freq="2H").strftime('Y%-%m-%d %H:%M:%S'), size=5),
'b': np.random.choice(pd.date_range("2019-01-01", "2019-12-31", freq="2H").strftime('Y%-%m-%d %H:%M:%S'), size=5),
'c': np.random.choice(pd.date_range("2019-01-01", "2019-12-31", freq="2H").strftime('Y%-%m-%d %H:%M:%S'), size=5)
})
df.loc[:, 'a':'c'] = df.loc[:, 'a':'c'].apply(lambda x: pd.to_datetime(x).dt.date)
a b c
0 2019-04-19 2019-08-11 2019-11-01
1 2019-04-02 2019-07-04 2019-08-29
2 2019-03-02 2019-06-02 2019-11-25
3 2019-08-22 2019-04-27 2019-04-28
4 2019-05-10 2019-09-21 2019-12-07
Upvotes: 2