Строки в pandas.Series всегда меньше 0?

1

Когда я оцениваю следующий фрагмент кода, я получаю значение True

import pandas as pd
df = pd.DataFrame({'a': ['assa', '100', 'AJSAND']})
(df < 0).all()

Всегда ли так, что строки оцениваются меньше нуля в Pandas DataFrame?

Однако следующие результаты в ошибке

's' < 0
Теги:
pandas

1 ответ

2
Лучший ответ

Если вы проверите исходный код pandas, вы можете найти следующие строки в методе _comp_method_FRAME. Вы можете найти полное объяснение здесь. Таким образом, речь идет скорее о способе сравнивать все типы типов, не приводя к исключению.

def _comp_method_FRAME(cls, func, special):
    str_rep = _get_opstr(func, cls)
    op_name = _get_op_name(func, special)

    @Appender('Wrapper for comparison method {name}'.format(name=op_name))
    def f(self, other):
        if isinstance(other, ABCDataFrame):
            # Another DataFrame
            if not self._indexed_same(other):
                raise ValueError('Can only compare identically-labeled '
                                 'DataFrame objects')
            return self._compare_frame(other, func, str_rep)

        elif isinstance(other, ABCSeries):
            return _combine_series_frame(self, other, func,
                                         fill_value=None, axis=None,
                                         level=None, try_cast=False)
        else:

            # straight boolean comparisons we want to allow all columns
            # (regardless of dtype to pass thru) See #4537 for discussion.
            res = self._combine_const(other, func,
                                      errors='ignore',
                                      try_cast=False)
            return res.fillna(True).astype(bool)

    f.__name__ = op_name

    return f

Таким образом, в основном dataframe сначала заполняется NaN, которые позже удаляются с истинным булевым!

  • 1
    Спасибо за ответ. Полное объяснение в приведенной вами ссылке прояснило, что « сравнения возвращают True по умолчанию (если сравнение не удается, например, fillna (True)) ».

Ещё вопросы

Сообщество Overcoder
Наверх
Меню