时间序列数据中的ValueError

时间:2019-12-30 09:44:20

标签: python python-3.x pandas datetime

我有以下代码:

import fxcmpy
import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
from datetime import date
import numpy as np
TOKEN = "hidden "
con = fxcmpy.fxcmpy(access_token=TOKEN, log_level='error')
print(con.get_instruments())
symbols = con.get_instruments()
ticker = 'JPYBasket'
start = datetime.datetime(2008,1,1)
end = datetime.datetime.today()
today = date.today()
start1 = datetime.datetime(2019,1,1)
data = con.get_candles(ticker, period='D1', start = start, end = end)
data1 = con.get_candles(ticker, period='D1', start = start1, end = end)
data.index = pd.to_datetime(data.index, format ='%Y-%m-%d')
data1.index = pd.to_datetime(data1.index, format ='%Y-%m-%d')

由于某种原因,我收到以下错误:

ValueError: cannot reindex from a duplicate axis

如果我说使用欧元/美元,我不会得到价值错误,但是只有当我尝试使用“ EMBasket”,“ JPYBasket”时,我才会收到此错误!

- JPY篮子:

data output is :
    bidopen bidclose    bidhigh bidlow  askopen askclose    askhigh asklow  tickqty
date                                    
2019-04-25  9720    9710    9714    9710    9723    9718    9719    9714    2363
2019-04-26  9720    9720    9720    9720    9723    9723    9723    9723    0
2019-04-26  9710    9698    9729    9679    9718    9708    9732    9686    74942
2019-04-29  9698    9679    9701    9661    9708    9683    9708    9664    66167
2019-04-30  9679    9669    9721    9666    9683    9674    9723    9668    72255
... ... ... ... ... ... ... ... ... ...
2019-12-19  9869    9889    9904    9849    9875    9893    9907    9852    75057
2019-12-20  9889    9888    9904    9879    9893    9897    9907    9882    72982
2019-12-23  9888    9896    9911    9880    9897    9901    9914    9883    73520
2019-12-24  9896    9898    9908    9894    9901    9907    9911    9897    64976
2019-12-26  9898    9851    9881    9846    9907    9856    9884    9849    40177
177 rows × 9 columns

data1的输出是:

    bidopen bidclose    bidhigh bidlow  askopen askclose    askhigh asklow  tickqty
date                                    
2019-04-25  9720    9710    9714    9710    9723    9718    9719    9714    2363
2019-04-26  9720    9720    9720    9720    9723    9723    9723    9723    0
2019-04-26  9710    9698    9729    9679    9718    9708    9732    9686    74942
2019-04-29  9698    9679    9701    9661    9708    9683    9708    9664    66167
2019-04-30  9679    9669    9721    9666    9683    9674    9723    9668    72255
... ... ... ... ... ... ... ... ... ...
2019-12-19  9869    9889    9904    9849    9875    9893    9907    9852    75057
2019-12-20  9889    9888    9904    9879    9893    9897    9907    9882    72982
2019-12-23  9888    9896    9911    9880    9897    9901    9914    9883    73520
2019-12-24  9896    9898    9908    9894    9901    9907    9911    9897    64976
2019-12-26  9898    9851    9881    9846    9907    9856    9884    9849    40177
177 rows × 9 columns

1 个答案:

答案 0 :(得分:4)

您的脚本缺少您对数据矩阵执行的操作。请更新问题,以了解对矩阵执行的操作。

在互联网上对ValueError: cannot reindex from a duplicate axis here at Ahere at B进行的一些搜索都指向操作前后数据集中的轴列中重复值的方向。

例如,来自(B)的示例,请参见下文。

Timestamp                                A      B      C     ...     
2014-11-09 00:00:00                     NaN     1      NaN   NaN      
2014-11-09 00:00:00                      2     NaN     NaN   NaN             
2014-11-09 00:00:00                     NaN    NaN     3     NaN   
2014-11-09 08:24:00                     NaN    NaN     1     NaN         
2014-11-09 08:24:00                     105    NaN     NaN   NaN           
2014-11-09 09:19:00                     NaN    NaN     23    NaN

尽管以下数据不同:

2014-11-09 08:24:00                     NaN    NaN     1     NaN         
2014-11-09 08:24:00                     105    NaN     NaN   NaN

在两种情况下,其列名称均为2014-11-09 08:24:00,这是不允许的。因此,ValueError: cannot reindex from a duplicate axis被引发为错误。当然,这也可能发生在列名中。

要具体说明您的情况:

示例数据中行标签2019-04-26至少存在两次。您可以做的是扩展标签,或者研究标签在欧元期间的外观。然后查看将数据转换为日元时更改行标签名称的操作。