Fix Python – Return datetime object of previous month

If only timedelta had a month argument in it’s constructor. So what’s the simplest way to do this?
EDIT: I wasn’t thinking too hard about this as was pointed out below. Really what I wanted was any day in the last month because eventually I’m going to grab the year and month only. So given a datetime object, what’s the simplest way to return an….

Fix Python – python date of the previous month

I am trying to get the date of the previous month with python.
Here is what i’ve tried:
str( time.strftime(‘%Y’) ) + str( int(time.strftime(‘%m’))-1 )

However, this way is bad for 2 reasons: First it returns 20122 for the February of 2012 (instead of 201202) and secondly it will return 0 instead of 12 on January.
I have solved this trouble in bas….

Fix Python – Add missing dates to pandas dataframe

My data can have multiple events on a given date or NO events on a date. I take these events, get a count by date and plot them. However, when I plot them, my two series don’t always match.
idx = pd.date_range(df[‘simpleDate’].min(), df[‘simpleDate’].max())
s = df.groupby([‘simpleDate’]).size()

In the above code idx becomes a range of say 30….

Fix Python – Difference between two dates in Python

I have two different dates and I want to know the difference in days between them. The format of the date is YYYY-MM-DD.
I have a function that can ADD or SUBTRACT a given number to a date:
def addonDays(a, x):
ret = time.strftime(“%Y-%m-%d”,time.localtime(time.mktime(time.strptime(a,”%Y-%m-%d”))+x*3600*24+3600))
return ret

where A is….

Fix Python – How can I parse a time string containing milliseconds in it with python?

I am able to parse strings containing date/time with time.strptime
>>> import time
>>> time.strptime(’30/03/09 16:31:32′, ‘%d/%m/%y %H:%M:%S’)
(2009, 3, 30, 16, 31, 32, 0, 89, -1)

How can I parse a time string that contains milliseconds?
>>> time.strptime(’30/03/09 16:31:32.123′, ‘%d/%m/%y %H:%M:%S’)
Traceback (most recent call last):
File “

Fix Python – SQLAlchemy default DateTime

This is my declarative model:
import datetime
from sqlalchemy import Column, Integer, DateTime
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Test(Base):
__tablename__ = ‘test’

id = Column(Integer, primary_key=True)
created_date = DateTime(default=datetime.datetime.utcnow)

However, when I tr….