I have a dataframe that comes from a database like this: Both FltDate and ESTAD2 are datetime64[ns]
>>> print df[['Airport', 'FltDate', 'Carrier', 'ESTAD2']]
Airport FltDate Carrier ESTAD2
0 EDI 2017-06-18 BACJ 1899-12-30 05:35:00
1 EDI 2017-06-18 BA 1899-12-30 06:40:00
2 EDI 2017-06-18 BACJ 1899-12-30 07:00:00
3 EDI 2017-06-18 BA 1899-12-30 07:05:00
4 EDI 2017-06-18 BA 1899-12-30 09:00:00
5 EDI 2017-06-18 I2 1899-12-30 11:05:00
6 EDI 2017-06-18 BA 1899-12-30 11:25:00
7 EDI 2017-06-18 BA 1899-12-30 13:45:00
>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 214 entries, 0 to 213
Data columns (total 12 columns):
Airport 214 non-null object
FltDate 214 non-null datetime64[ns]
<snip>
ESTAD2 214 non-null datetime64[ns]
<snip>
dtypes: datetime64[ns](4), object(8)
memory usage: 20.1+ KB
How to get the Date Part from FltDate and Time Part from ESTAD2.
Result should be like this say for Row 0
2017-06-18 05:35:00 (that is 18Jun2017 of FltDate + 05:35 of ESTAD2)
I may replace ESTAD2 with above result.. or create a new column as FltDateTime.
Tried various ways and failed... like below... adding was unsuccessful.
>>> df.FltDate.dt.date
0 2017-06-18
1 2017-06-18
2 2017-06-18
>>> df.ESTAD2.dt.time
0 05:35:00
1 06:40:00
2 07:00:00
You can use strftime
with to_datetime
:
df['date'] = pd.to_datetime(df.FltDate.dt.strftime('%Y-%m-%d ') +
df.ESTAD2.dt.strftime('%H:%M:%S'))
print (df)
Airport FltDate Carrier ESTAD2 date
0 EDI 2017-06-18 BACJ 1899-12-30 05:35:00 2017-06-18 05:35:00
1 EDI 2017-06-18 BA 1899-12-30 06:40:00 2017-06-18 06:40:00
2 EDI 2017-06-18 BACJ 1899-12-30 07:00:00 2017-06-18 07:00:00
3 EDI 2017-06-18 BA 1899-12-30 07:05:00 2017-06-18 07:05:00
4 EDI 2017-06-18 BA 1899-12-30 09:00:00 2017-06-18 09:00:00
5 EDI 2017-06-18 I2 1899-12-30 11:05:00 2017-06-18 11:05:00
6 EDI 2017-06-18 BA 1899-12-30 11:25:00 2017-06-18 11:25:00
7 EDI 2017-06-18 BA 1899-12-30 13:45:00 2017-06-18 13:45:00
Alternative solution:
df['date'] = pd.to_datetime(df.FltDate.dt.strftime('%Y-%m-%d ') +
df.ESTAD2.astype(str).str.split().str[1])
print (df)
Airport FltDate Carrier ESTAD2 date
0 EDI 2017-06-18 BACJ 1899-12-30 05:35:00 2017-06-18 05:35:00
1 EDI 2017-06-18 BA 1899-12-30 06:40:00 2017-06-18 06:40:00
2 EDI 2017-06-18 BACJ 1899-12-30 07:00:00 2017-06-18 07:00:00
3 EDI 2017-06-18 BA 1899-12-30 07:05:00 2017-06-18 07:05:00
4 EDI 2017-06-18 BA 1899-12-30 09:00:00 2017-06-18 09:00:00
5 EDI 2017-06-18 I2 1899-12-30 11:05:00 2017-06-18 11:05:00
6 EDI 2017-06-18 BA 1899-12-30 11:25:00 2017-06-18 11:25:00
7 EDI 2017-06-18 BA 1899-12-30 13:45:00 2017-06-18 13:45:00
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.