Date object with year and month only

import datetime

date = datetime.date(year=2013, month=1, day=4)
str(date.year) + '-' + str(date.month)

No, you can't do that. For your usecase, use a tuple instead:

key = (2013, 1)

Since you don't need to do date manipulations on the value a tuple more than suffices.


As an addition to other answer, you can use namedtuple.

from collections import namedtuple
MyDate = namedtuple('MyDate', ['month', 'year'])
dkey = MyDate(year=2013, month=1)

If you want to use datetime, you must follow its attributes. Here I quote it from the official website:

"An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes: year, month, and day."

So, you can't ignore day and remember to give assignment.