简体   繁体   English

calendar.monthrange() - ValueError:系列的真值不明确。 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

[英]calendar.monthrange() - ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

I have a pandas dataframe that has a date field.我有一个带有日期字段的熊猫数据框。 I'm trying to use this field to calculate the number of days in the month of that date.我正在尝试使用此字段来计算该日期所在月份的天数。 However, when I use the code below, I get the following error:但是,当我使用下面的代码时,出现以下错误:

ValueError: The truth value of a Series is ambiguous. ValueError:系列的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all()使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

from calendar import monthrange
df['NumDays'] = monthrange(df['Date_Field'].map(lambda x: x.year),df['Date_Field'].map(lambda x: x.month))[1]

I've tested to see if the .year and .month are working by creating two separate columns in the DF with that data.我已经通过在 DF 中使用该数据创建两个单独的列来测试 .year 和 .month 是否正常工作。

df['year'] = df['Date_Field'].map(lambda x: x.year)
df['month'] = df['Date_Field'].map(lambda x: x.month)
df['NumDays'] = monthrange(df['year'], df['month'])[1]

The resulting 'year' column had the correct year, and the 'month' column had the correct month, but when I tried to use them in monthrange() I get the same ValueError.结果“年”列有正确的年份,“月”列有正确的月份,但是当我尝试在 monthrange() 中使用它们时,我得到了相同的 ValueError。

Any guidance would be appreciated.任何指导将不胜感激。

Thanks.谢谢。

Use days_in_month attribute of DatetimeIndex:使用 DatetimeIndex 的days_in_month属性:

df = pd.DataFrame({'date':pd.date_range('01/01/2019', periods=12, freq='MS')})
df['No of Days in this Month'] = df['date'].dt.days_in_month
df

Output:输出:

         date  No of Days in this Month
0  2019-01-01                        31
1  2019-02-01                        28
2  2019-03-01                        31
3  2019-04-01                        30
4  2019-05-01                        31
5  2019-06-01                        30
6  2019-07-01                        31
7  2019-08-01                        31
8  2019-09-01                        30
9  2019-10-01                        31
10 2019-11-01                        30
11 2019-12-01                        31

Can be something like this:可以是这样的:

# dt is referred to datetime
df['NumDays'] = df['Date_Field'].dt.days_in_month

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Series 的真值是不明确的。 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() - Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() 系列的真实值是不明确的。 使用a.empty,a.bool(),a.item(),a.any()或a.all() - The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() Pandas 系列:ValueError:系列的真值不明确。 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() - Pandas Series: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() 熊猫中的ValueError:系列的真值不明确。 使用a.empty,a.bool(),a.item(),a.any()或a.all() - ValueError in pandas: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() ValueError: ('Series 的真值不明确。使用 a.empty, a.bool(), a.item(), a.any() 或 a.all()。', 'occurred at index 0' ) - ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0') 如何解决ValueError:系列的真值不明确。 使用a.empty,a.bool(),a.item(),a.any()或a.all() - How to resolve ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() 一行 if 语句,ValueError: 系列的真值不明确。 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() - one line if statement, ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() If-else ValueError系列的真值不明确。 使用a.empty,a.bool(),a.item(),a.any()或a.all() - If-else ValueError The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() Pandas ValueError:系列的真值不明确。 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() - Pandas ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() ValueError:Series 的真值不明确。 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。 & - ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). &
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM