Home > Net >  Sorting and Combining datetime data frames Pandas Python
Sorting and Combining datetime data frames Pandas Python

Time:10-30

I am trying to combine dates1 and dates2 that combines the data frames sorts them in synchronous order. There are alike date values on both sets and the code should be able to only have one unique value in the output. what code will I need to get the Expected Output below?

import pandas as pd 
import numpy as np 

dates1 = pd.to_datetime(['2015-10-08 13:41:00', 
                 '2015-10-08 13:44:00', '2015-10-08 13:48:00',
                 '2015-10-08 13:49:00'])

dates2 = pd.to_datetime(['2015-10-08 13:42:00','2015-10-08 13:44:00', '2015-10-08 13:45:00', '2015-10-08 13:50:00'])

Expected output:

['2015-10-08 13:41:00',
 '2015-10-08 13:42:00', '2015-10-08 13:43:00',
 '2015-10-08 13:44:00', '2015-10-08 13:45:00',
 '2015-10-08 13:46:00', '2015-10-08 13:47:00',
 '2015-10-08 13:48:00', '2015-10-08 13:49:00',
 '2015-10-08 13:50:00', '2015-10-08 13:51:00']

CodePudding user response:

Since they're both pandas.DatetimeIndex, you can use set operations from pandas.Index

dates1.union(dates2)
      

For multiple DatetimeIndex there are several options

dates1.union(dates2).union(dates3).union(dates4)

or a better approach

import functools
functools.reduce(pd.Index.union, [dates1, dates2, dates3, dates4])

or use the concat approach suggested by the other answer to this question

CodePudding user response:

pd.concat([date1,date2]).drop_duplicates().sort_values()
  • Related