Subtracting two columns - error

Screen Link:

My Code:

dete_resignations['institute_service'] = dete_resignations['cease_date'] - dete_resignations['dete_start_date']


What I expected to happen:
I expect to get data in institute service column

What actually happened:

ypeErrorTraceback (most recent call last)
/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ in na_op(x, y)
    675         try:
--> 676             result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs)
    677         except TypeError:

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/computation/ in evaluate(op, op_str, a, b, use_numexpr, **eval_kwargs)
    203     if use_numexpr:
--> 204         return _evaluate(op, op_str, a, b, **eval_kwargs)
    205     return _evaluate_standard(op, op_str, a, b)

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/computation/ in _evaluate_standard(op, op_str, a, b, **eval_kwargs)
     63     with np.errstate(all='ignore'):
---> 64         return op(a, b)

TypeError: unsupported operand type(s) for -: 'float' and 'str'

During handling of the above exception, another exception occurred:

TypeErrorTraceback (most recent call last)
<ipython-input-37-35f85473bd17> in <module>()
      2 # Calculate the length of time an employee spent in their respective workplace and create a new column
----> 3 dete_resignations['institute_service'] = dete_resignations['cease_date'] - dete_resignations['dete_start_date']
      5 dete_resignations['institute_service'].head()

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ in wrapper(left, right, name, na_op)
    737                 lvalues = lvalues.values
--> 739         result = wrap_results(safe_na_op(lvalues, rvalues))
    740         return construct_result(
    741             left,

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ in safe_na_op(lvalues, rvalues)
    698         try:
    699             with np.errstate(all='ignore'):
--> 700                 return na_op(lvalues, rvalues)
    701         except Exception:
    702             if isinstance(rvalues, ABCSeries):

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ in na_op(x, y)
    680                 result = np.empty(x.size, dtype=dtype)
    681                 mask = notna(x) & notna(y)
--> 682                 result[mask] = op(x[mask], _values_from_object(y[mask]))
    683             elif isinstance(x, np.ndarray):
    684                 result = np.empty(len(x), dtype=x.dtype)

TypeError: unsupported operand type(s) for -: 'float' and 'str'

Have also google about subtraction, but was not able to find, what i doing wrong, please help to solve this

hey @vinodgchandaliya

have you tried to resolve this - TypeError: unsupported operand type(s) for -: 'float' and 'str'

can you check what are the datatypes for these columns?

  • dete_resignations[‘cease_date’]
  • dete_resignations[‘dete_start_date’]

The datatypes need to be same for the subtraction to happen.

1 Like

have check datatype one is float and other is object, will let you know what is the output after converting both to common datatype

1 Like

@Rucha Thank you so much, Have converted datatype for dete_resignations['dete_start_date'] =dete_resignations['dete_start_date'].astype("float") now able to subtract :slight_smile: :grinning:

1 Like