Я использую pandas для некоторых преобразований в таблице:
desired.head(2)
Я использую эту функцию для получения списка дат, которые я хочу получить для других вещей позже:
date_list2018 = []
date_list2019 = []
date_list2020 = []
def list_creator(row):
if row['is_exclusive']==1:
numpy_array = (np.arange(row['start_date'],row['end_date'], dtype='datetime64[D]'))
if row['year_start'] ==2018:
for numpy in numpy_array:
b = numpy.astype(datetime)
timestring = b.strftime('%Y-%m-%d')
date_list2018.append(timestring)
elif row['year_start'] ==2019:
for numpy in numpy_array:
b = numpy.astype(datetime)
timestring = b.strftime('%Y-%m-%d')
date_list2019.append(timestring)
else:
for numpy in numpy_array:
b = numpy.astype(datetime)
timestring = b.strftime('%Y-%m-%d')
date_list2020.append(timestring)
desired.apply(list_creator, axis=1)
Я читал, что pandas быстрее, если я выполняю функции векторизации, прямо сейчас я использую метод применения, поэтому я попробовал чтобы написать векторизованную функцию:
date_list2018 = []
date_list2019 = []
date_list2020 = []
def list_creator_iterator(exclusive, start_date,end_date, year_start):
if exclusive==1:
numpy_array = (np.arange(start_date,end_date, dtype='datetime64[D]'))
if year_start ==2018:
for numpy in numpy_array:
b = numpy.astype(datetime)
timestring = b.strftime('%Y-%m-%d')
date_list2018.append(timestring)
elif year_start ==2019:
for numpy in numpy_array:
b = numpy.astype(datetime)
timestring = b.strftime('%Y-%m-%d')
date_list2019.append(timestring)
else:
for numpy in numpy_array:
b = numpy.astype(datetime)
timestring = b.strftime('%Y-%m-%d')
date_list2020.append(timestring)
list_creator_iterator(desired['is_exclusive'].values, desired['start_date'].values,desired['end_date'].values
,desired['year_start'].values)
Но я получаю эту ошибку:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-73-e658d035b9fa> in <module>
23
24 list_creator_iterator(desired['is_exclusive'].values, desired['start_date'].values,desired['end_date'].values
---> 25 ,desired['year_start'].values)
<ipython-input-73-e658d035b9fa> in list_creator_iterator(exclusive, start_date, end_date, year_start)
4 def list_creator_iterator(exclusive, start_date,end_date, year_start):
5
----> 6 if exclusive==1:
7 numpy_array = (np.arange(start_date,end_date, dtype='datetime64[D]'))
8 if year_start ==2018:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Что я делаю не так?