无法将PCA的numpy数组转换为熊猫数据帧

时间:2020-06-01 02:59:15

标签: pandas

我正在尝试将2000年1月1日到2019年12月31日之间的新西兰(NZ)的6小时平均海平面压力(mslp)值转换为一个,对其执行PCA,然后使用k均值聚类以确定大气环流模式的聚类。我已经成功完成了PCA,但是当我使用以下方法将PCA(numpy.ndarray)转换为熊猫数据帧时:

    PCdf = pd.DataFrame(data = PCs,
                index = dset['time'],
                columns = ["PC%s" % (x) for x in range(1, PCs.shape[1] +1)])

我回来了:

AssertionError: <class 'xarray.core.dataarray.DataArray'>

完整的错误输出如下:

    ---------------------------------------------------------------------------
    AssertionError                            Traceback (most recent call 
    last)
    <ipython-input-309-c5cf2ed6ff1b> in <module>
          1 PCdf = pd.DataFrame(data = PCs,
    ----> 2                     index = dset['time'])

    /opt/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in 
    __init__(self, data, index, columns, dtype, copy)
        438                 mgr = init_dict({data.name: data}, index, columns, dtype=dtype)
        439             else:
    --> 440                 mgr = init_ndarray(data, index, columns, 
    dtype=dtype, copy=copy)
        441 
        442         # For data is list-like, or Iterable (will consume into 
            list)

    /opt/anaconda3/lib/python3.7/site- 
    packages/pandas/core/internals/construction.py in init_ndarray(values, 
    index, columns, dtype, copy)
        182                 raise_with_traceback(e)
        183 
    --> 184     index, columns = _get_axes(*values.shape, index=index, 
    columns=columns)
        185     values = values.T
        186 

    /opt/anaconda3/lib/python3.7/site- 
   packages/pandas/core/internals/construction.py in _get_axes(N, K, index, 
    columns)
        425         index = ibase.default_index(N)
        426     else:
    --> 427         index = ensure_index(index)
        428 
        429     if columns is None:

    /opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in 
    ensure_index(index_like, copy)
       5742         return index_like
       5743     if hasattr(index_like, "name"):
    -> 5744         return Index(index_like, name=index_like.name, copy=copy)
       5745 
       5746     if is_iterator(index_like):

    /opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in 
    __new__(cls, data, dtype, copy, name, fastpath, tupleize_cols, **kwargs)
        344             else:
        345                 result = DatetimeIndex(
    --> 346                     data, copy=copy, name=name, dtype=dtype, 
    **kwargs
        347                 )
        348                 return result

    /opt/anaconda3/lib/python3.7/site- 
   packages/pandas/core/indexes/datetimes.py in __new__(cls, data, freq, 
    start, end, periods, tz, normalize, closed, ambiguous, dayfirst, 
    yearfirst, dtype, copy, name, verify_integrity)
        332             yearfirst=yearfirst,
        333             ambiguous=ambiguous,
    --> 334             int_as_wall_time=True,
        335         )
        336 

    /opt/anaconda3/lib/python3.7/site- 
   packages/pandas/core/arrays/datetimes.py in _from_sequence(cls, data, 
     dtype, copy, tz, freq, dayfirst, yearfirst, ambiguous, int_as_wall_time)
        444             yearfirst=yearfirst,
        445             ambiguous=ambiguous,
    --> 446             int_as_wall_time=int_as_wall_time,
        447         )
        448 

    /opt/anaconda3/lib/python3.7/site-        
    packages/pandas/core/arrays/datetimes.py in sequence_to_dt64ns(data, 
    dtype, copy, tz, dayfirst, yearfirst, ambiguous, int_as_wall_time)
       1916         result = result.copy()
       1917 
    -> 1918     assert isinstance(result, np.ndarray), type(result)
       1919     assert result.dtype == "M8[ns]", result.dtype
       1920 

    AssertionError: <class 'xarray.core.dataarray.DataArray'>

我想知道是否有人可以确定我在这里做错了什么。任何帮助将不胜感激。

0 个答案:

没有答案