3

I am using Pandas to create a new column in a data frame created from a csv.

[in] DfT_raw = pd.read_csv('./file.csv', index_col = False)
[in] print(DfT_raw)

[out]            Region Name dCount ONS    CP  S Ref E  S Ref N   Road  \
0        East Midlands  E06000015      14/04/00 00:00  37288   434400   336000   A516   
1        East Midlands  E06000015       14/04/00 00:00  37288   434400   336000   A516   
2        East Midlands  E06000015       14/04/00 00:00  37288   434400   336000   A516   
3        East Midlands  E06000015       14/04/00 00:00  37288   434400   336000   A516   

I define a function to strip the time from the datetime fieldn (dCount) and then create a new column 'date'

[in] def date_convert(dCount):
         return dCount.date()

     DfT_raw['date'] = DfT_raw.apply(lambda row: date_convert(row['dCount']), axis=1)

[out] AttributeError: ("'str' object has no attribute 'date'", u'occurred at index 0')

There is some issue with the index_col. I previously used index_col = 1 but got the same error.

When I print 'dCount' I get

0          14/04/00 00:00
1          14/04/00 00:00
2          14/04/00 00:00
3          14/04/00 00:00
4          14/04/00 00:00

The index column is causing the error. How do I ensure this isn't given to the function?

2
  • 1
    It's a str not a datetime object, you need to convert first df['dCount'] = pd.to_datetime(df['dCount']) and then get the date df['dCount'].dt.date, you could have read those datetime strings in as datetime by passing parse_dates=['dCount'] to read_csv Commented Oct 14, 2015 at 9:02
  • DfT_raw = pd.read_csv('./file.csv', parse_dates=['dCount'],index_col = False) works perfect, many thanks! Feel free to post as answer if you wish Commented Oct 14, 2015 at 10:43

1 Answer 1

4

Your error here is that your dates are str not datetime, either convert using to_datetime:

df['dCount'] = pd.to_datetime(df['dCount'])

or better just tell read_csv to parse that column as datetime:

DfT_raw = pd.read_csv('./file.csv', parse_dates=['dCount'],index_col = False)

Afterwards you can then get just the date by calling the dt.date accessor

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.