0
flights['Duration']=flights['Duration'].str.replace('h','*60').str.replace(' ','+').str.replace('m','*1').apply(eval)

However I am getting this error message:

AttributeError                            Traceback (most recent call last)
    <ipython-input-24-45eafc3e9d23> in <module>()
    ----> 1 travel['Duration']=travel['Duration'].str.replace('h','*60').str.replace(' ','+').str.replace('m','*1').apply(eval)
          2 
    3 frames
    /usr/local/lib/python3.6/dist-packages/pandas/core/strings.py in _validate(data)
       1965 
       1966         if inferred_dtype not in allowed_types:
    -> 1967             raise AttributeError("Can only use .str accessor with string " "values!")
       1968         return inferred_dtype
       1969 

    AttributeError: Can only use .str accessor with string values!
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
Priya
  • 1
  • 1
  • 1
  • 3
    Does this answer your question? [AttributeError: Can only use .str accessor with string values, which use np.object\_ dtype in pandas](https://stackoverflow.com/questions/52065909/attributeerror-can-only-use-str-accessor-with-string-values-which-use-np-obje) – Guy Dec 02 '19 at 13:40

3 Answers3

0

Looks like your flights[ 'Duration'] column have not string value (probably its int64 or float64).

import pandas as pd
df = pd.DataFrame(data=[(1, '12h 50m'), (2, '1h 12m')], columns=['id', 'Duration'])
df['Duration']=df['Duration'].str.replace('h','*60').str.replace(' ','+').str.replace('m','*1').apply(eval)

Result:

   id  Duration
0   1       770
1   2        72

This is works well because when we create a dataframe - second column values was string type. If we have non-consistency in base data so we should be careful with results, It can be misleading. Example:

df = pd.DataFrame(data=[(1, 123), (2, 0.123), (3, '12h 30m')], columns=['id', 'Duration'])
df['Duration']=df['Duration'].astype(str).str.replace('h','*60').str.replace(' ','+').str.replace('m','*1').apply(eval)

Result:

   id  Duration
0   1   123.000
1   2     0.123
2   3   750.000

In this example we used .astype(str), but as we see If data have various datatypes - result may be really wrong. So check your datasource for dataframe and then try transform data again :)

Jefferson Houp
  • 858
  • 1
  • 7
  • 17
0

What you probably want to do is more like:

flights['Duration'] = pd.to_timedelta(flights['Duration']).seconds//60

which will directly convert the time string to minutes

The method you're trying to use, with apply(eval) seems . . . very unsafe.

Daniel F
  • 13,620
  • 2
  • 29
  • 55
-1

You should first cast string to the column, using astype(str).

Oleg O
  • 1,005
  • 6
  • 11