简体   繁体   English

python pandasquantlib.time.date.Date

[英]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. 我的问题是-列'ColDate'是字符串类型,另一列是日期类型,我无法验证'ColStr'= A是匹配项,而'ColStr'='B'是不匹配项。

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. 我将不得不(1)将y.ColDate转换为Date(2)或将x.ColDate转换为与y.ColDate相似的格式的str。

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. 我猜想您需要使用数据dataset1['ColDate'] = dataset1.ColDate.map(convert_type)或其他任何方法来将它们转换为单个通用类型,以迭代列值。 Check other functions from pandas docs like apply() . pandas文档中检查其他函数,例如apply()

The convert_type function should be defined in your program and accept a single argument to be passed into map() . convert_type函数应在您的程序中定义,并接受一个传递给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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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