Reputation: 586
I have two dataframes:
import pandas as pd
from quantlib.time.date import Date
cols = ['ColStr','ColDate']
dataset1 = [['A',Date(2017,1,1)],['B',Date(2017,2,2)]]
x = pd.DataFrame(dataset1,columns=cols)
dataset2 = [['A','2017-01-01'],['B','2017-02-04']]
y = pd.DataFrame(dataset2,columns=cols)
Now, I want to compare the two table. I have written another set of code that compares the two (larger) dataframes and works for strings and numerical value.
My problem is - with column 'ColDate' one being string type and other being Date type, I am not able to validate if 'ColStr' = A is a match and 'ColStr' = 'B' is a mismatch.
I would have to (1) either convert y.ColDate to Date (2) or convert x.ColDate to str with a similar format as y.ColDate.
How do I achieve one or the other
Upvotes: 0
Views: 484
Reputation: 656
You probably want to use the dt.strftime() function.
dataset1[0].dt.strftime("%Y-%m-%d")
Upvotes: 0
Reputation: 1612
I guess that you need to cast them to a single common type using something like dataset1['ColDate'] = dataset1.ColDate.map(convert_type)
or any other method to iterate column values. Check other functions from pandas docs like apply()
.
The convert_type
function should be defined in your program and accept a single argument to be passed into map()
.
And, when the columns have same types, you can compare them using any method you like.
Upvotes: 1