代码之家  ›  专栏  ›  技术社区  ›  junkone

未正确分析datecolumn

  •  0
  • junkone  · 技术社区  · 9 年前

    我正在将csv导入熊猫数据帧。它无法正确导入“我的日期”列。

    Ticker,Date_Time,Close,volume rate,Volatility,breakouthappened,prevdaycci negative,stopprice,day1gainpotential,day1closegainpotential,day2gainpotential,day3gainpotential,day4gainpotential,breakoutpoint,#
    

    AAAP,2016年12月27日,26.29263,6.12,0,-165.77,23.51,-2.49,-4.95,-2.53,-2.71,0.51,24.39,1

    potential_trades_df=pd.read_csv('C:\\Users\\Ramesh\\PycharmProjects\\DemoTest\\TradeSignals.csv',parse_dates=True,keep_date_col = True)
    currenttrade=potential_trades_df.iloc[0]
    
    
    >>> currenttrade.Date_Time.strptime('%Y-%m-%d')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'str' object has no attribute 'strptime'
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   anon    9 年前

    我对此不太有经验。但我可以给你一些帮助我解决这个问题的东西。 您可以编写解析器方法,并在读取时将字符串转换为datetime对象。

    date_format = '%Y-%m-%d'
    parser = lambda x: pd .datetime.strptime(x, date_format)
    

    当你读到它的时候就叫它。(假设包含日期的列是索引0。

    potential_trades_df=pd.read_csv('/path/to/file.csv',parse_dates=[0], date_parser=parser)
    

    编辑:您在代码中编写的日期格式似乎与示例中的日期格式不同。在您的示例中,date_格式为 "%m/%d/%Y"