在将日期转换为日/周/月/年的日期时,“ ValueError:无法将浮点NaN转换为整数”

时间:2019-03-12 11:42:28

标签: python-3.x pandas pandas-groupby

在处理具有很大数据的熊猫数据框的日期列时,出现错误:

df1['REQUESTTIMESTAMP'] = pd.to_datetime(df['REQUESTTIMESTAMP'],format= '%d-%b-%y %H.%M.%S.%f')

转换日期(天/周/月/年)时。我必须按天/周/月/格式分开,并据此计算相应的ID。

我遵循以下流程:

  1. 要以日期格式转换列:
    df1['Day/Week/Month/Year'] = df['REQUESTTIMESTAMP'].apply(lambda x: "%d/%d/%d/%d" % (x.day, x.week, x.month, x.year))

  2. 以日期/星期/月份/格式分隔日期

    `File "<pyshell#333>", line 1, in <lambda> df1['Day/Week/Month/Year'] = df1['REQUESTTIMESTAMP'].apply(lambda x: "%d/%d/%d/%d" % (x.day, x.week, x.month, x.year)) ValueError: cannot convert float NaN to integer`

执行此操作时出现错误:

df1.groupby('Day/Week/Month/Year') ['CONVERSATIONID'].agg(['count', 'nunique'])

如果错误已修复,则必须使用以下方式按周,月,年对ID进行分组: count nunique Day/Week/Month/Year 12/41/10/2018 12 12 24/43/10/2018 2 2 30/44/10/2018 3 3 Original input file: 1050 12-OCT-18 17.58.09.822000000<<coming correct 1055 15-OCT-18 17.02.05.512000000<<Nat 1058 15-OCT-18 17.10.55.264000000<<Nat 1061 15-OCT-18 17.12.18.139000000<<Nat Format of date column in my df coming as Nat: 10 1046 ... 12/40/10/2018 11 1050 ... 12/40/10/2018 12 1055 ... NaT 13 1058 ... NaT 14 1061 ... NaT 15 1064 ... 24/42/10/2018 16 1067 ... 24/42/10/2018 17 1070 ... 30/43/10/2018 18 1073 ... 30/43/10/2018 19 1076 ... 30/43/10/2018 20 1078 ... NaT 21 1081 ... NaT 22 1083 ... NaT 23 1086 ... NaT 24 1089 ... NaT 25 1096 ... NaT

预期o / p:

df1.groupby('Day/Week/Month/Year') ['CONVERSATIONID'].agg(['count', 'nunique'])
                     count  nunique
Day/Week/Month/Year                
12/40/10/2018           12       12
24/42/10/2018            2        2
30/43/10/2018            3        3
NaT                  39518    15376

使用strftime的O / P:

<ProductCard v-for="product in products.data"
  :product="product"
  :key="product.id"
  :userСurrency="userСurrency"
  :user-currency-code="userCurrencyCode" 
  :price="`prices.product_${product.id}.price`" />   

在转换后如何解决NaT出现的错误和日期方面,您将获得任何帮助?

0 个答案:

没有答案