pivotstarttime["tutorialafterlevelchoice"] = (pivotstarttime["tutorialstart"] -…
- User
pivot_start_time["tutorial_after_level_choice"] = (pivot_start_time["tutorial_start"] - pivot_start_time["level_choice"]) start_tutorial_after_level_choice = pivot_start_time[pivot_start_time["tutorial_after_level_choice"]>0].index.nunique()
ошибка InvalidComparison Traceback (most recent call last) File c:\Users\Владимир\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\arrays\datetimelike.py:1054, in DatetimeLikeArrayMixin._cmp_method(self, other, op) 1053 try: -> 1054 other = self._validate_comparison_value(other) 1055 except InvalidComparison:
File c:\Users\Владимир\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\arrays\datetimelike.py:588, in DatetimeLikeArrayMixin._validate_comparison_value(self, other) 587 elif not is_list_like(other): --> 588 raise InvalidComparison(other) 590 elif len(other) != len(self):
InvalidComparison: 0
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last) d:\загружено\Project_3-Markin.ipynb Ячейка 45 line 2 1 pivot_start_time["tutorial_after_level_choice"] = (pivot_start_time["tutorial_start"] - pivot_start_time["level_choice"]) ----> 2 start_tutorial_after_level_choice = pivot_start_time[pivot_start_time["tutorial_after_level_choice"]>0].index.nunique()
File c:\Users\Владимир\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\ops\common.py:72, in _unpack_zerodim_and_defer..new_method(self, other) 68 return NotImplemented 70 other = item_from_zerodim(other) ---> 72 return method(self, other) ... 35 typ = type(right).name ---> 36 raise TypeError(f"Invalid comparison between dtype={left.dtype} and {typ}") 37 return res_values
TypeError: Invalid comparison between dtype=timedelta64[ns] and int
исправить
- Assistant
start_tutorial_after_level_choice = pivot_start_time[pd.to_timedelta(pivot_start_time["tutorial_after_level_choice"])>pd.to_timedelta(0)].index.nunique()
Также необходимо убедиться, что столбцы "tutorial_start" и "level_choice" имеют тип данных datetime. Если это не так, их нужно преобразовать, например, с помощью метода pd.to_datetime().
- User
ошибка скорей во второй строке кода, а тип данных datetime64[ns] start_tutorial_after_level_choice = pivot_start_time[pivot_start_time[“tutorial_after_level_choice”]>0].index.nunique()
- Assistant
Да, вы правы, ошибка действительно возникает из-за использования оператора сравнения ">0" для столбца "tutorial_after_level_choice", который имеет тип данных timedelta64[ns].
Для исправления ошибки можно использовать метод "dt.total_seconds()", чтобы преобразовать значения timedelta в секунды, и затем сравнить с нулем:
start_tutorial_after_level_choice = pivot_start_time[pivot_start_time["tutorial_after_level_choice"].dt.total_seconds() > 0].index.nunique()