Python使用if函数:ValueError:Series的真实值不明确.使用a.empty,a.bool(),a.item(),a.any()或a.all() [英] Python Use if function: ValueError:Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

查看:694
本文介绍了Python使用if函数:ValueError:Series的真实值不明确.使用a.empty,a.bool(),a.item(),a.any()或a.all()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前曾问过这个问题,但是每种情况都不一样...我的请求是这样的:

I know this question was asked before but each case is different... My plea is this:

df = pd.read_csv(‘file.csv’)
# convert the string into a datetime object
time = pd.to_datetime(df.dttm_utc)
Month=time.dt.month
Day=time.dt.day
Hour=time.dt.Hour
InDayLightSavings=True
if (Month<3): InDayLightSavings=False
if (Month==3) and (Day<11) and (Hour<2): InDayLightSavings=False
if (Month>11): InDayLightSavings=False
if (Month==11) and (Day>4)and (Hour>=2): InDayLightSavings=False

if (InDayLightSavings):
    time=time-datetime.timedelta(hours=1)

正如您正确猜到的那样,它返回的是系列"的真值"(Truth value)是模棱两可的.使用a.empty,a.bool(),a.item(),a.any()或a.all().我将其与时间戳一起使用,之前将其更改为ISO8601,并且此方法有效,但显然不适用于系列.我尝试添加.any(),但它不起作用.我也更改为&如其他线程中所建议. 我的file.csv的参数看起来像这样,一直运行到2012年年底:

And it returns, as you guessed correctly,Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). I used this with timestamp, changing it to an ISO8601 before and this method works but it doesn't work for series apparently. And I tried adding .any() and it doesn't work. I also change and to & as suggested in other thread. A par of my file.csv looks like this, running til end of 2012:

timestamp	  dttm_utc	          value
1325376300	2012-01-01 0:05:00	16.9444
1325376600	2012-01-01 0:10:00	16.6837
1325376900	2012-01-01 0:15:00	16.6837
1325377200	2012-01-01 0:20:00	16.9444
1325377500	2012-01-01 0:25:00	16.1623
1325377800	2012-01-01 0:30:00	16.6837

所需的输出: 包含是一个以15分钟为间隔的数据示例

Desired output: Include is an example of data in 15 min interval

3/13/2016 1:00	51
3/13/2016 1:15	48
3/13/2016 1:30	50.4
3/13/2016 1:45	51
3/13/2016 3:00	47.4
3/13/2016 3:15	49.8
3/13/2016 3:30	51
3/13/2016 3:45	51
3/13/2016 4:00	48.6

感谢您的帮助.谢谢!

推荐答案

您看到的异常是由于您尝试根据一组单一条件来评估具有许多不同条目的系列. 简要地,让我们看一下您的操作:

The exception you are seeing is due to the fact that you try to evaluate a series with many different entries against a set of single conditions. Briefly, let's have a look what you do:

错误分析(为什么不那样做):

首先,您确实获取了pandas数据框列,然后将其转换为日期时间,当然它也返回了列(系列).

time = pd.to_datetime(df.dttm_utc) # Convert content of dttm_utc COLUMN to datetime
                                   # This returns a dataframe COLUMN / series
Month = time.dt.month              # Convert content of your COLUMN/series to month
Day = time.dt.day                  # Convert content of your COLUMN/series to month
Hour = time.dt.Hour                # Convert content of your COLUMN/series to month

您的错误:然后尝试评估系列中的特定条件:

if (Month == whatever_condition): 
    do_something()

但是,您不能将单个条件与一系列条件进行比较,至少不能这样. Python不知道您指的是该系列中的哪个条目,因为其中的某些值可能与其他值不同.这意味着,对于该系列中的某些项目,条件可能会得到满足,而对于其他项目则不能.因此,ValueError: The truth value of a series is ambiguous.

However, you can't compare a single condition to a series, at least not like that. Python doesn't know which entry in the series you mean, as some values in it may be different to others. That means, for some items in the series the condition may be fulfilled, for others not. Hence the ValueError: The truth value of a series is ambiguous.

您要做什么:

逐项评估,理想情况下以矢量化方式进行.我的建议:始终保持在熊猫数据框中:

Evaluate item by item, ideally in a vectorized way. My suggestion: stay in the pandas dataframe all time:

df['Datetime'] = pd.to_datetime(df['dttm_utc']) # Add second column with datetime format
df['Month'] = df.Datetime.dt.month     # Extract month and add to new column
                                                # Same for day
df.loc[(df.Month < 3), 'InDayLightSavings'] = False 
# You can add multiple conditions here
# Finally, your filter:
df.loc[(df.InDayLightSavings == True), 'Time'] = df['Time'] - dt.timedelta(hours=1) 
# dt when import datetime as dt, else just datetime

进一步阅读此处此处在这里.

这篇关于Python使用if函数:ValueError:Series的真实值不明确.使用a.empty,a.bool(),a.item(),a.any()或a.all()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆