我有一个Pandas数据帧,它是通过使用QPython从KDB数据库获取数据而生成的。在
首先,日期列作为一个奇怪的数据类型返回:dtype('<M8[ns]')
df = conn.sync("select Date, Open, High, Low, Close from stocktable", pandas=True)
df["Date"].dtype
# dtype('<M8[ns]')
但是,当我检查列的内容时,最下面一行将数据类型显示为datetime。在
^{pr2}$另外,方法last()
工作不正常。我要求提供过去5个月的数据,但是所有的数据都被返回了。在
# Expected to only return last 5 months of data, but returns it all.
df.set_index("Date").last("5M")
如何获取此数据帧的最后一行?
对我来说很好:
它还可以很好地处理重复项,只需要排序
^{pr2}$DateTime
列:这对我来说很管用。在
演示:
解决了。问题是KDB返回的数据是按DESC顺序排序的,这混淆了方法
last()
。在解决方案是在查询中添加一个sort子句(在Q语言中,它带有一个backtick followed by the keyword xasc)
或者,对Pandas数据框中的数据进行排序。在
^{pr2}$相关问题 更多 >
编程相关推荐