operator + method

Series operator +(
  1. dynamic other
)

Addition (+) operator:

Adds the corresponding elements of this Series and another Series or a numeric value.

  • If other is a Series, handles index alignment.
  • If other is a num, adds that value to each element in the Series.
  • For categorical Series, the result will be converted to object dtype.

Implementation

Series operator +(dynamic other) {
  if (other is num) {
    List<dynamic> resultData = [];
    for (int i = 0; i < length; i++) {
      var val = data[i];
      if (val == _getMissingRepresentation(this)) {
        resultData.add(_getMissingRepresentation(this));
      } else {
        try {
          resultData.add(val + other);
        } catch (e) {
          resultData.add(_getMissingRepresentation(this));
        }
      }
    }
    var result =
        Series(resultData, name: "$name + $other", index: index.toList());
    // Arithmetic operations on categorical data should result in non-categorical Series
    result._dtype = 'object';
    return result;
  } else if (other is Series) {
    var result = _performArithmeticOperation(other, (a, b) => a + b, '+');
    // Arithmetic operations on categorical data should result in non-categorical Series
    result._dtype = 'object';
    return result;
  }
  throw Exception("Can only add Series to Series or num.");
}