1

ie)

        count
2015-01  2
2015-02  1
2015-03  4

for the group i tried pd.groupby(b,by=[b.index.month,b.index.year])

but there was object has no attribute 'month' error

2
  • 1
    Are those actual date objects or strings? Commented Apr 18, 2017 at 18:49
  • date is coloumn in dataframe objects Commented Apr 18, 2017 at 18:56

1 Answer 1

1

Apply sorted with the key parameter set to pd.to_datetime

df.assign(date=df.date.apply(sorted, key=pd.to_datetime))

  id                                  date
0  a              [2015-02-01, 2015-03-01]
1  b                          [2015-03-01]
2  s              [2015-01-01, 2015-03-01]
3  f  [2015-01-01, 2015-01-01, 2015-03-01]

Then use pd.value_counts

pd.value_counts(pd.to_datetime(df.date.sum()).strftime('%Y-%m'))

2015-03    4
2015-01    3
2015-02    1
dtype: int64

debugging

You should be able to copy and paste this code... please verify that it runs as expected.

import pandas as pd

df = pd.DataFrame(dict(
        id=list('absf'),
        date=[
            ['2015-03-01', '2015-02-01'],
            ['2015-03-01'],
            ['2015-01-01', '2015-03-01'],
            ['2015-01-01', '2015-01-01', '2015-03-01']
        ]
    ))[['id', 'date']]

print(df.assign(date=df.date.apply(sorted, key=pd.to_datetime)))
print()
print(pd.value_counts(pd.to_datetime(df.date.sum()).strftime('%Y-%m')))

You should expect to see

  id                                  date
0  a              [2015-02-01, 2015-03-01]
1  b                          [2015-03-01]
2  s              [2015-01-01, 2015-03-01]
3  f  [2015-01-01, 2015-01-01, 2015-03-01]

2015-03    4
2015-01    3
2015-02    1
dtype: int64
Sign up to request clarification or add additional context in comments.

5 Comments

I don't understand what the problem is? You showed a dataframe df. Did you run this code on df? You also did not give enough information to tell what is going on.
@d.doo see the debugging section of my updated answer.
df.assign(dt=df.dt.apply(sorted, key=pd.to_datetime)) there was TypeError: Unrecognized value type: <class 'str'> and ValueError: Given date string not likely a datetime.
Can you edit your question with the full stack trace?
Debugging code works ok for me pd.__version__ 0.19.2 - d.doo could you just confirm if the debugging code works correctly please. The problem may be with your source data. pd.to_datetime also has errors='coerce' and errors='ignore' that you could try.

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.