序列长度与np不匹配,其中

2024-10-02 12:34:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据框架MSYs_init,它包含一个现有的列“Start Date”,大约有250个值。我想从该列的日期中选取年份,并将其与一系列赠款开始日期中的年份相匹配。请看下面,因为我相信我已经把事情复杂化了,无法通过谷歌fu获得解决方案

MSYs_init['Start Date'] = pd.to_datetime(MSYs_init['Start Date'], errors='coerce')
VISTAMbrStartYr = pd.DatetimeIndex(MSYs_init['Start Date']).year


VISTAGrantYrStarts = pd.Series(['2020, 8, 17','2019, 8, 18', '2018, 8, 19', '2017, 9, 17'])
VISTAGrantYrStarts = pd.to_datetime(VISTAGrantYrStarts, errors='coerce')
VISTAGrantYr = pd.DatetimeIndex(VISTAGrantYrStarts).year  

MSYs_init['VISTA Grant Year'] = np.where(VISTAMbrStartYr == VISTAGrantYr, VISTAGrantYrStarts, np.nan)

这是我的错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-129-27a1a1454145> in <module>
     12 VISTAGrantYr = pd.DatetimeIndex(VISTAGrantYrStarts).year
     13 
---> 14 MSYs_init['Mbr Starting Grant Yr'] = np.where((VISTAMbrStartYr == VISTAGrantYr) &
     15                                               (VISTAMbrStartMoDay >= VISTAGrantYrStartMoDay),
     16                                               VISTAMbrStartYr,VISTAMbrStartYr-1) 

~\anaconda3\envs\PythonData\lib\site-packages\pandas\core\indexes\base.py in cmp_method(self, other)
    100         if isinstance(other, (np.ndarray, Index, ABCSeries)):
    101             if other.ndim > 0 and len(self) != len(other):
--> 102                 raise ValueError("Lengths must match to compare")
    103 
    104         if is_object_dtype(self) and not isinstance(self, ABCMultiIndex):

ValueError: Lengths must match to compare

Tags: toselfdateinitnpyearstartpd
1条回答
网友
1楼 · 发布于 2024-10-02 12:34:50

如果我理解正确,您试图实现的目标可以通过以下方式实现:

MSYs_init['Start Date'] = pd.to_datetime(MSYs_init['Start Date'], errors='coerce')
VISTAGrantYrStarts = pd.to_datetime(VISTAGrantYrStarts, errors='coerce')
VISTAGrantYr = VISTAGrantYrStarts.year
MSYs_init['VISTA Grand Year'] = np.where(MSYs_init['Start Date'].dt.year.isin(VISTAGrantYr),
                                         MSYs_init['Start Date'].dt.year,
                                         np.nan)

工作示例:

import pandas as pd
import numpy as np
df=pd.DataFrame({'index':[0,1,2,3],
                 'dates':['2019-02-20','2019-02-21','2020-02-21','2021-02-20']})
df['dates'] = pd.to_datetime(df['dates'],errors='coerce')
aux = pd.to_datetime(['2017-02-20','2018-02-21','2019-02-21','2019-02-20'],errors='coerce').year
df['dates_2'] = np.where(df['dates'].dt.year.isin(aux),df['dates'].dt.year,np.nan)

输出:

   index      dates  dates_2
0      0 2019-02-20   2019.0
1      1 2019-02-21   2019.0
2      2 2020-02-21      NaN
3      3 2021-02-20      NaN

@Celius,我需要它来返回VISTAGrantYrStarts,而不是“开始日期”,所以当我尝试这样做时,它不起作用

MSYs_init['Start Date'] = pd.to_datetime(MSYs_init['Start Date'],errors='coerce')
GrantStarts = pd.to_datetime(['2017-02-20','2018-02-21','2019-02-21','2019-02-20'],errors='coerce').year
MSYs_init['Mbr Grant Start'] = np.where(MSYs_init['Start Date'].dt.year.isin(GrantStarts),GrantStarts.dt.year,np.nan)
MSYs_init.head(50)

                                     -
AttributeError                            Traceback (most recent call last)
<ipython-input-71-52fd06b959bf> in <module>
      1 MSYs_init['Start Date'] = pd.to_datetime(MSYs_init['Start Date'],errors='coerce')
      2 GrantStarts = pd.to_datetime(['2017-02-20','2018-02-21','2019-02-21','2019-02-20'],errors='coerce').year
  > 3 MSYs_init['Mbr Grant Start'] = np.where(MSYs_init['Start Date'].dt.year.isin(GrantStarts),GrantStarts.dt.year,np.nan)
      4 MSYs_init.head(50)

AttributeError: 'Int64Index' object has no attribute 'dt'

相关问题 更多 >

    热门问题