熊猫:date_range错误

时间:2012-08-06 07:47:34

标签: python date pandas

我使用pandas date_range函数收到错误。我已经给出了下面的描述,并且可以提供更多的背景,但它似乎是我真正需要深入挖掘自己来解决的问题。

所以我想知道的是,如果有一种方法可以获取跟踪中提到的文件的来源,特别是datetime.pyx,或者定义了Timestamp类的地方 - 我似乎无法找到它。我对Python非常环保并且不了解包裹和来源的方式,但是我希望如果我能深入挖掘一下,我将能够对此有所了解。

>>> d=process_files(args, options, False)
Processing file K2.csv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/py7041Jtp.py", line 158, in process_files
  File "/tmp/py7041Jtp.py", line 81, in process_csv
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.1-py2.7-linux-x86_64.egg/pandas/tseries/index.py", line 1317, in date_range
    freq=freq, tz=tz, normalize=normalize, name=name)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.1-py2.7-linux-x86_64.egg/pandas/tseries/index.py", line 176, in __new__
    tz=tz, normalize=normalize)
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.1-py2.7-linux-x86_64.egg/pandas/tseries/index.py", line 254, in _generate
    start = Timestamp(start)
  File "datetime.pyx", line 87, in pandas.lib.Timestamp.__new__ (pandas/src/tseries.c:26892)
  File "datetime.pyx", line 511, in pandas.lib.convert_to_tsobject (pandas/src/tseries.c:32019)
  File "datetime.pyx", line 640, in pandas.lib._string_to_dts (pandas/src/tseries.c:33452)

5 个答案:

答案 0 :(得分:1)

要查找datetime.pyx,我建议从:

开始
find /usr/local/lib/python2.7 -name 'datetime.pyx'

要查找Timestamp类的定义,我将以:

开头
grep -r 'class Timestamp' /usr/local/lib/python2.7/dist-packages/pandas-0<tab>

答案 1 :(得分:1)

.pyx个文件是Cython文件。您可能会发现它们已安装,但如果您没有从源构建库,它们可能不会。但是,所有的pandas源都可以在github上找到。 datetime.pyx的来源为here

Cython看起来与Python类似,所以如果您只是尝试浏览源代码来找出导致错误的原因,您可能会发现查看Cython源代码很有用。但是你不能像普通的Python一样调试它,因为它不是。

答案 2 :(得分:1)

为方便起见,我复制了下面的pandas.lib._string_to_dts功能代码。 根据您的追溯,这是抛出异常的那个。

cdef inline _string_to_dts(object val, pandas_datetimestruct* dts):
  cdef:
      npy_bool islocal, special
      PANDAS_DATETIMEUNIT out_bestunit
      int result

  if PyUnicode_Check(val):
      val = PyUnicode_AsASCIIString(val);

  result = parse_iso_8601_datetime(val, len(val), PANDAS_FR_ns,
                                   NPY_UNSAFE_CASTING,
                                   dts, &islocal, &out_bestunit, &special)
  if result == -1:
      raise ValueError('Unable to parse %s' % str(val))

对我而言,您似乎向pandas.date_range提供了无法解析为日期的输入,而process_csv中的代码会引发ValueError异常,如下例所示。

>>> import pandas
>>> pandas.date_range('hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python2.7/site-packages/pandas-0.8.2.dev_90842ba-py2.7-linux-x86_64.egg/pandas/tseries/index.py", line 1317, in date_range
    freq=freq, tz=tz, normalize=normalize, name=name)
  File "../lib/python2.7/site-packages/pandas-0.8.2.dev_90842ba-py2.7-linux-x86_64.egg/pandas/tseries/index.py", line 176, in __new__
    tz=tz, normalize=normalize)
  File ".../lib/python2.7/site-packages/pandas-0.8.2.dev_90842ba-py2.7-linux-x86_64.egg/pandas/tseries/index.py", line 254, in _generate
    start = Timestamp(start)
  File "datetime.pyx", line 87, in pandas.lib.Timestamp.__new__ (pandas/src/tseries.c:26892)
  File "datetime.pyx", line 511, in pandas.lib.convert_to_tsobject (pandas/src/tseries.c:32019)
  File "datetime.pyx", line 640, in pandas.lib._string_to_dts (pandas/src/tseries.c:33452)
ValueError: Unable to parse hello

答案 3 :(得分:0)

原来我只是根据默认为None的命令开关解析错误的字段。没想清楚:/

答案 4 :(得分:0)

请在GitHub(或邮件列表)上发布潜在的错误报告。谢谢 - 虽然这确实得到了比GitHub更多的答案。