问:大家好,在python 中,i 介于a和b之间,写成 a< =i <=b 或 i>=a and i<=b,还有其他的写法吗?不用大于小于等于号
#pip install interval
from interval import Interval
#生成的默认区间是左闭右闭
interval_2_5 = Interval(2, 5)
print(interval_2_5)
2.34 in interval_2_5 #反馈结果True
如果想改成左开或者右开区间,可以通过lower_closed和upper_closed参数设定
具体可以查看Interval这个类的帮助文件,部分截图如下
all = Interval.all()
| >>> lt = Interval.less_than(10)
| >>> le = Interval.less_than_or_equal_to(10)
| >>> some = Interval(10, 20, lower_closed=False)
| >>> single = Interval.equal_to(10)
| >>> ge = Interval.greater_than_or_equal_to(10)
| >>> gt = Interval.greater_than(10)
| >>> ne = Interval.equal_to(17)
| >>> 10 in all
| True
| >>> 10 in lt
| False
| >>> 10 in le
| True
| >>> 10 in some
| False
| >>> 10 in single
| True
| >>> 10 in ge
| True
| >>> 10 in gt
| False
| >>> 10 in ne
| False








你的错误在于这一句:
b = CustomBusinessDay(holidays) CustomBusinessDay这个类里面的holidays参数是一个关键字参数,不是一个位置参数,所以你进行实例化类的时候应该这样写
b = CustomBusinessDay(holidays=holidays)
这个类的部分帮助文件如下
help(CustomBusinessDay)
Help on class CustomBusinessDay in module pandas.tseries.offsets:
class CustomBusinessDay(_CustomMixin, BusinessDay)
| CustomBusinessDay(n=1, normalize=False, weekmask='Mon Tue Wed Thu Fri', holidays=None, calendar=None, offset=datetime.timedelta(0))
|
| DateOffset subclass representing possibly n custom business days,
| excluding holidays.
int() argument must be a string, a bytes-like object or a number, not 'list'
During handling of the above exception, another exception occurred:
TypeError: `n` argument must be an integer, got <class 'list'>
TypeError Traceback (most recent call last)pandas\_libs\tslibs\offsets.pyx in pandas._libs.tslibs.offsets.BaseOffset._validate_n()TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' During handling of the above exception, another exception occurred:TypeError Traceback (most recent call last)<ipython-input-100-eab4994fcd77> in <module> 22 count_businessday(start_day,end_day) 23 ---> 24 sheet['工作日'] = sheet.apply(lambda x:count_businessday(x['出院时间'],x['归档日期']),axis=1)D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, raw, result_type, args, **kwds) 7546 kwds=kwds, 7547 )-> 7548 return op.get_result() 7549 7550 def applymap(self, func) -> "DataFrame":D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self) 178 return self.apply_raw() 179 --> 180 return self.apply_standard() 181 182 def apply_empty_result(self):D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self) 269 270 def apply_standard(self):--> 271 results, res_index = self.apply_series_generator() 272 273 # wrap resultsD:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self) 298 for i, v in enumerate(series_gen): 299 # ignore SettingWithCopy here in case the user mutates--> 300 results[i] = self.f(v) 301 if isinstance(results[i], ABCSeries): 302 # If we have a view on v, we need to make a copy because<ipython-input-100-eab4994fcd77> in <lambda>(x) 22 count_businessday(start_day,end_day) 23 ---> 24 sheet['工作日'] = sheet.apply(lambda x:count_businessday(x['出院时间'],x['归档日期']),axis=1)<ipython-input-100-eab4994fcd77> in count_businessday(start_day, end_day) 4 '2020-10-05','2020-10-06','2020-10-07','2020-10-08'] 5 holidays=[pd.Timestamp(i) for i in holidays]----> 6 b = CustomBusinessDay(holidays) 7 bus_day = pd.date_range(start=start_day, end=end_day, freq=b) 8 length = len(bus_day)pandas\_libs\tslibs\offsets.pyx in pandas._libs.tslibs.offsets.CustomBusinessDay.__init__()pandas\_libs\tslibs\offsets.pyx in pandas._libs.tslibs.offsets.BusinessMixin.__init__()pandas\_libs\tslibs\offsets.pyx in pandas._libs.tslibs.offsets.BaseOffset.__init__()pandas\_libs\tslibs\offsets.pyx in pandas._libs.tslibs.offsets.BaseOffset._validate_n()TypeError: `n` argument must be an integer, got <class 'list'>
from pandas.tseries.offsets import CustomBusinessDay def count_businessday(start_day,end_day): holidays=['2020-04-06','2020-05-01','2020-05-04','2020-05-05','2020-06-25','2020-10-01', '2020-10-05','2020-10-06','2020-10-07','2020-10-08'] holidays=[pd.Timestamp(i) for i in holidays] b = CustomBusinessDay(holidays) bus_day = pd.date_range(start=start_day, end=end_day, freq=b) length = len(bus_day) extra_work_day = ['2020-04-26','2020-05-09','2020-06-28','2020-09-27','2020-10-10'] extra_work_day = [pd.Timestamp(i) for i in extra_work_day] extra_len = 0 for i in extra_work_day: if i >= start_day and i <= end_day: extra_len = extra_len+1 return(length+extra_len-1) if __name__=='__main__': count_businessday(start_day,end_day) sheet['工作日'] = sheet.apply(lambda x:count_businessday(x['出院时间'],x['归档日期']),axis=1)

问题在于你的extra_work_day= ['2020-04-26','2020-05-09','2020-06-28','2020-09-27','2020-10-10'] 这个列表里面的元素是字符串,所以不能和时间戳对象进行比较,你需要变一下,把他变成时间戳对象,可以通过下面的方式: extra_work_day= ['2020-04-26','2020-05-09','2020-06-28','2020-09-27','2020-10-10']
extra_work_day=[pd.Timestamp(i) for i in extra_work_day]
然后你就可以用原来的命令比较啦
from pandas.tseries.offsets import CustomBusinessDay def count_businessday(start_day,end_day): b = CustomBusinessDay(holidays=['2020-04-06','2020-05-01','2020-05-04','2020-05-05','2020-06-25','2020-10-01', '2020-10-05','2020-10-06','2020-10-07','2020-10-08']) bus_day = pd.date_range(start=start_day, end=end_day, freq=b) length = len(bus_day) extra_work_day = ['2020-04-26','2020-05-09','2020-06-28','2020-09-27','2020-10-10'] extra_len = 0 dayday = Interval(start_day,end_day) for i in extra_work_day: if i in dayday: extra_len = extra_len+1 return(length+extra_len-1) if __name__=='__main__': count_businessday(start_day,end_day)
sheet['工作日'] = sheet.apply(lambda x:count_businessday(x['出院时间'],x['归档日期']),axis=1)
出现这个错误:
'<' not supported between instances of 'str' and 'Timestamp'
from pandas.tseries.offsets import CustomBusinessDay
def count_businessday(start_day,end_day):
b = CustomBusinessDay(holidays=['2020-04-06','2020-05-01','2020-05-04','2020-05-05','2020-06-25','2020-10-01',
'2020-10-05','2020-10-06','2020-10-07','2020-10-08'])
bus_day = pd.date_range(start=start_day, end=end_day, freq=b)
length = len(bus_day)
extra_work_day = ['2020-04-26','2020-05-09','2020-06-28','2020-09-27','2020-10-10']
extra_len = 0
dayday = Interval(start_day,end_day)
for i in extra_work_day:
if i in dayday:
extra_len = extra_len+1
return(length+extra_len-1)
if __name__=='__main__':
count_businessday(start_day,end_day)
sheet['工作日'] = sheet.apply(lambda x:count_businessday(x['出院时间'],x['归档日期']),axis=1)
sheet.head()
出现这个错误:
'<' not supported between instances of 'str' and 'Timestamp'