How to add items into a numpy array

Appending data to an existing array is a natural thing to want to do for anyone with python experience. However, if you find yourself regularly appending to large arrays, you’ll quickly discover that NumPy doesn’t easily or efficiently do this the way a python list will. You’ll find that every “append” action requires re-allocation of … Read more

Efficient date range overlap calculation?

Determine the latest of the two start dates and the earliest of the two end dates. Compute the timedelta by subtracting them. If the delta is positive, that is the number of days of overlap. Here is an example calculation: >>> from datetime import datetime >>> from collections import namedtuple >>> Range = namedtuple(‘Range’, [‘start’, … Read more

How do I convert strings in a Pandas data frame to a ‘date’ data type?

Essentially equivalent to @waitingkuo, but I would use pd.to_datetime here (it seems a little cleaner, and offers some additional functionality e.g. dayfirst): In [11]: df Out[11]: a time 0 1 2013-01-01 1 2 2013-01-02 2 3 2013-01-03 In [12]: pd.to_datetime(df[‘time’]) Out[12]: 0 2013-01-01 00:00:00 1 2013-01-02 00:00:00 2 2013-01-03 00:00:00 Name: time, dtype: datetime64[ns] In … Read more

What is %timeit in Python?

%timeit is an IPython magic function, which can be used to time a particular piece of code (a single execution statement, or a single method). From the documentation: %timeit Time execution of a Python statement or expression Usage, in line mode: %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement To use it, for example if … Read more

Forced naming of parameters in Python

In Python 3 – Yes, you can specify * in the argument list. From docs: Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed used keyword arguments. >>> def foo(pos, *, forcenamed): … print(pos, forcenamed) … >>> foo(pos=10, forcenamed=20) 10 20 >>> foo(10, forcenamed=20) 10 20 >>> foo(10, 20) Traceback (most … Read more

python pandas extract year from datetime: df[‘year’] = df[‘date’].year is not working

If you’re running a recent-ish version of pandas then you can use the datetime attribute dt to access the datetime components: In [6]: df[‘date’] = pd.to_datetime(df[‘date’]) df[‘year’], df[‘month’] = df[‘date’].dt.year, df[‘date’].dt.month df Out[6]: date Count year month 0 2010-06-30 525 2010 6 1 2010-07-30 136 2010 7 2 2010-08-31 125 2010 8 3 2010-09-30 84 … Read more

Using a dictionary to select function to execute

Simplify, simplify, simplify: def p1(args): whatever def p2(more args): whatever myDict = { “P1”: p1, “P2”: p2, … “Pn”: pn } def myMain(name): myDict[name]() That’s all you need. You might consider the use of dict.get with a callable default if name refers to an invalid function— def myMain(name): myDict.get(name, lambda: ‘Invalid’)() (Picked this neat trick … Read more