1

How do i concat part of a date with a file name after doing groupby.
I want to end up with an array: 'hello Jan 2 2014','hello Jan 2 2014'

The results of my code were a surprise.

import pandas as pd
from datetime import datetime
d = {   'File' : pd.Series(['hello', 'what']), 
    'Status' : pd.Series([0., 0.]), 
    'Error' : pd.Series([2., 2.]), 
    'AlertDays' : pd.Series([2., 2.]), 
    'Date' : pd.Series([datetime(2014, 1, 2), datetime(2014, 1, 2)])}
df=pd.DataFrame(d)
df['Date']=pd.to_datetime(df['Date'])
Faildf=df[df.Status==0]
Fx=Faildf.groupby('File')['Date'].max().reset_index()
Fx['concat']=Fx['File'] +' '+ str(Fx['Date'])
#FailArray=Fx['concat'].unique()

why is there more than one date... i thought i lost the others dates by doing the groupby and max? results:

>>> Fx
    File                Date                                             concat
0  hello 2012-05-02 00:00:00  0   2012-05-02 00:00:00\n1   2012-05-02 00:00:...
1   what 2012-05-02 00:00:00  0   2012-05-02 00:00:00\n1   2012-05-02 00:00:...

1 Answer 1

1

The problem is that you are concatenating a pandas Series Fx['File'] with the string representation of a pandas Series str(Fx['Date']), what you need to do is apply the str cast function to the elements of Fx['Date'] like this:

>>> Fx['File'] + " " + Fx['Date'].apply(str)
0    hello 2014-01-02 00:00:00
1     what 2014-01-02 00:00:00
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.