熊猫时间序列文档示例不起作用

时间:2016-06-24 18:32:19

标签: python pandas

我正试图从pandas docs work:

中创建这个示例
In [28]: df = pd.DataFrame({'year': [2015, 2016],
   ....:                    'month': [2, 3],
   ....:                    'day': [4, 5],
   ....:                    'hour': [2, 3]})
   ....: 

In [29]: pd.to_datetime(df)
Out[29]: 
0   2015-02-04 02:00:00
1   2016-03-05 03:00:00
dtype: datetime64[ns]

和此:

In [30]: pd.to_datetime(df[['year', 'month', 'day']])
Out[30]: 
0   2015-02-04
1   2016-03-05
dtype: datetime64[ns]

在这两种情况下,我都收到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/ann/anaconda3/lib/python3.5/site-packages/pandas/tseries/tools.py in _convert_listlike(arg, box, format, name)
    379             try:
--> 380                 values, tz = tslib.datetime_to_datetime64(arg)
    381                 return DatetimeIndex._simple_new(values, name=name, tz=tz)

pandas/tslib.pyx in pandas.tslib.datetime_to_datetime64 (pandas/tslib.c:26925)()

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-93-7cad31999ec7> in <module>()
----> 1 pd.to_datetime(df)

/Users/ann/anaconda3/lib/python3.5/site-packages/pandas/util/decorators.py in wrapper(*args, **kwargs)
     87                 else:
     88                     kwargs[new_arg_name] = new_arg_value
---> 89             return func(*args, **kwargs)
     90         return wrapper
     91     return _deprecate_kwarg

/Users/ann/anaconda3/lib/python3.5/site-packages/pandas/tseries/tools.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, coerce, unit, infer_datetime_format)
    274     return _to_datetime(arg, errors=errors, dayfirst=dayfirst, yearfirst=yearfirst,
    275                         utc=utc, box=box, format=format, exact=exact,
--> 276                         unit=unit, infer_datetime_format=infer_datetime_format)
    277 
    278 

/Users/ann/anaconda3/lib/python3.5/site-packages/pandas/tseries/tools.py in _to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, freq, infer_datetime_format)
    393         return _convert_listlike(arg, box, format, name=arg.name)
    394     elif com.is_list_like(arg):
--> 395         return _convert_listlike(arg, box, format)
    396 
    397     return _convert_listlike(np.array([ arg ]), box, format)[0]

/Users/ann/anaconda3/lib/python3.5/site-packages/pandas/tseries/tools.py in _convert_listlike(arg, box, format, name)
    381                 return DatetimeIndex._simple_new(values, name=name, tz=tz)
    382             except (ValueError, TypeError):
--> 383                 raise e
    384 
    385     if arg is None:

/Users/ann/anaconda3/lib/python3.5/site-packages/pandas/tseries/tools.py in _convert_listlike(arg, box, format, name)
    370                                                  yearfirst=yearfirst, freq=freq,
    371                                                  unit=unit,
--> 372                                                  require_iso8601=require_iso8601)
    373 
    374             if com.is_datetime64_dtype(result) and box:

pandas/tslib.pyx in pandas.tslib.array_to_datetime (pandas/tslib.c:37142)()

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

我试图在Python 3 Jupyter笔记本中运行它。以下是有关我的设置的更多详细信息:

3.5.1 |Anaconda 2.5.0 (x86_64)| (default, Dec  7 2015, 11:24:55) 
[GCC 4.2.1 (Apple Inc. build 5577)]
0.17.1

对此有何见解?

2 个答案:

答案 0 :(得分:2)

您正在测试的BEGIN { require MyClass }的行为是new in v0.18.1。我的猜测是你使用的是旧版本。

答案 1 :(得分:0)

import sys
import pandas as pd

print sys.version
print pd.__version__

2.7.11 |Anaconda custom (x86_64)| (default, Dec  6 2015, 18:57:58) 
[GCC 4.2.1 (Apple Inc. build 5577)]
0.18.1

以下对我有用。

df = pd.DataFrame({'year': [2015, 2016],
                   'month': [2, 3],
                   'day': [4, 5],
                   'hour': [2, 3]})

print df
print
print pd.to_datetime(df)

   day  hour  month  year
0    4     2      2  2015
1    5     3      3  2016

0   2015-02-04 02:00:00
1   2016-03-05 03:00:00
dtype: datetime64[ns]