Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have this following datetime64[ns] type <class 'pandas.core.series.Series'> timestamps.

x1=pd.Timestamp('2018-04-25 00:00:00')

x2=pd.Timestamp('2020-09-02 00:53:00')

Can I replace the time part of x1 with x2 or vice versa?

The output should look something like this

datetime64[ns] type.

2018-04-25 00:53:00

1 Answer

0 votes
by (36.8k points)

If x1 and x2 are 2 individual series and of the equal indexes ,you can use series.dt accessor to parse dt.date from x1 and dt.time from x2 and bind to x1 after converting to string, then convert it back to datetime:

pd.to_datetime(x1.dt.date.astype(str) +' '+x2.dt.time.astype(str))

0   2018-04-25 00:53:00

dtype: datetime64[ns]

If you are a beginner and want to know more about Python the do check out the data science with python course 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...