0

I have a dataframe, df.

I want to replace the 7th to 5th from last character with a 0 if it's a /:

df['StartDate'].str[-7:-5]=df['StartDate'].str[-7:-5].str.replace('/', '0')

Returns the error:

TypeError: 'StringMethods' object does not support item assignment

Data looks like:

number                 StartDate     EndDate  Location_Id      Item_Id        xxx          yyy\
3                460    4/1/2012   4/11/2012         2502   3890004215         0            0
28              2731  10/17/2013  10/30/2013         3509   5100012114         0            0
34              1091   1/10/2013   1/23/2013         2544   5100012910         0            0
134             1630    5/2/2013   5/15/2013         2506  69511912000         0            0
138              327   1/12/2012   1/25/2012         5503   1380016686
5
  • Can you show a small example of what your data actually looks like? Preferably just print the first 10 items of the series Commented Oct 28, 2014 at 3:31
  • 1
    Thanks. Is your replace operation basically trying to make sure that the days in each date are zero-padded, so 4/1/2012 -> 4/01/2012? What date format are you trying to achieve? Commented Oct 28, 2014 at 3:42
  • That's exactly right, I actually want them to be in datetime format. But I don't think you can store a datetime object in a cell of a dataframe, so I was thinking 20120401 would be the easiest way to store it, but once I have the 0 from /, I could do the rest. Commented Oct 28, 2014 at 3:51
  • 1
    Pandas actually has builtin datetime support, you can do df['StartDate'] = pd.to_datetime(df['StartDate']), and then access datetime-methods via df.StartDate.dt.<methods> (although the .dt part might only work if you're using v0.15 of Pandas) Commented Oct 28, 2014 at 3:54
  • You sir, just saved me an immense amount of time and are a god. Answer and I will give you best. Commented Oct 28, 2014 at 4:10

1 Answer 1

1

Pandas has builtin support for datetime objects (pandas might have its own implementation rather than using the standard library's directly, but the idea is the same), so instead of trying to reformat dates using string methods, converting to datetime is much easier:

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

Once you've converted, there are some easy to use methods related to datetime objects that you can get at through the .dt accessor (may be a recent addition in v0.15):

df.StartDate.dt.month
Out[20]: 
3       4
28     10
34      1
134     5
138     1
dtype: int64
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.