简体   繁体   中英

python pandas quantlib.time.date.Date

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

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.

您可能要使用dt.strftime()函数。

dataset1[0].dt.strftime("%Y-%m-%d")

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM