0

使用 pandasdate_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)
4

5 回答 5

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>
于 2012-08-06T07:55:13.007 回答
1

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

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

于 2012-08-06T07:57:17.410 回答
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
于 2012-08-06T10:16:10.880 回答
0

原来我只是根据默认为None. 没有想清楚:/

于 2012-08-06T10:21:14.420 回答
0

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

于 2012-08-07T18:40:35.217 回答