pandas 0.9.1 documentation

Caveats and Gotchas

NaN, Integer NA values and NA type promotions

Choice of NA representation

For lack of NA (missing) support from the ground up in NumPy and Python in general, we were given the difficult choice between either

  • A masked array solution: an array of data and an array of boolean values indicating whether a value
  • Using a special sentinel value, bit pattern, or set of sentinel values to denote NA across the dtypes

For many reasons we chose the latter. After years of production use it has proven, at least in my opinion, to be the best decision given the state of affairs in NumPy and Python in general. The special value NaN (Not-A-Number) is used everywhere as the NA value, and there are API functions isnull and notnull which can be used across the dtypes to detect NA values.

However, it comes with it a couple of trade-offs which I most certainly have not ignored.

Support for integer NA

In the absence of high performance NA support being built into NumPy from the ground up, the primary casualty is the ability to represent NAs in integer arrays. For example:

In [424]: s = Series([1, 2, 3, 4, 5], index=list('abcde'))

In [425]: s
Out[425]: 
a    1
b    2
c    3
d    4
e    5

In [426]: s.dtype
Out[426]: dtype('int64')

In [427]: s2 = s.reindex(['a', 'b', 'c', 'f', 'u'])

In [428]: s2
Out[428]: 
a     1
b     2
c     3
f   NaN
u   NaN

In [429]: s2.dtype
Out[429]: dtype('float64')

This trade-off is made largely for memory and performance reasons, and also so that the resulting Series continues to be “numeric”. One possibility is to use dtype=object arrays instead.

NA type promotions

When introducing NAs into an existing Series or DataFrame via reindex or some other means, boolean and integer types will be promoted to a different dtype in order to store the NAs. These are summarized by this table:

Typeclass Promotion dtype for storing NAs
floating no change
object no change
integer cast to float64
boolean cast to object

While this may seem like a heavy trade-off, in practice I have found very few cases where this is an issue in practice. Some explanation for the motivation here in the next section.

Why not make NumPy like R?

Many people have suggested that NumPy should simply emulate the NA support present in the more domain-specific statistical programming langauge R. Part of the reason is the NumPy type hierarchy:

Typeclass Dtypes
numpy.floating float16, float32, float64, float128
numpy.integer int8, int16, int32, int64
numpy.unsignedinteger uint8, uint16, uint32, uint64
numpy.object_ object_
numpy.bool_ bool_
numpy.character string_, unicode_

The R language, by contrast, only has a handful of built-in data types: integer, numeric (floating-point), character, and boolean. NA types are implemented by reserving special bit patterns for each type to be used as the missing value. While doing this with the full NumPy type hierarchy would be possible, it would be a more substantial trade-off (especially for the 8- and 16-bit data types) and implementation undertaking.

An alternate approach is that of using masked arrays. A masked array is an array of data with an associated boolean mask denoting whether each value should be considered NA or not. I am personally not in love with this approach as I feel that overall it places a fairly heavy burden on the user and the library implementer. Additionally, it exacts a fairly high performance cost when working with numerical data compared with the simple approach of using NaN. Thus, I have chosen the Pythonic “practicality beats purity” approach and traded integer NA capability for a much simpler approach of using a special value in float and object arrays to denote NA, and promoting integer arrays to floating when NAs must be introduced.

Integer indexing

Label-based indexing with integer axis labels is a thorny topic. It has been discussed heavily on mailing lists and among various members of the scientific Python community. In pandas, our general viewpoint is that labels matter more than integer locations. Therefore, with an integer axis index only label-based indexing is possible with the standard tools like .ix. The following code will generate exceptions:

s = Series(range(5))
s[-1]
df = DataFrame(np.random.randn(5, 4))
df
df.ix[-2:]

This deliberate decision was made to prevent ambiguities and subtle bugs (many users reported finding bugs when the API change was made to stop “falling back” on position-based indexing).

Label-based slicing conventions

Non-monotonic indexes require exact matches

Endpoints are inclusive

Compared with standard Python sequence slicing in which the slice endpoint is not inclusive, label-based slicing in pandas is inclusive. The primary reason for this is that it is often not possible to easily determine the “successor” or next element after a particular label in an index. For example, consider the following Series:

In [430]: s = Series(randn(6), index=list('abcdef'))

In [431]: s
Out[431]: 
a    1.337122
b   -1.531095
c    1.331458
d   -0.571329
e   -0.026671
f   -1.085663

Suppose we wished to slice from c to e, using integers this would be

In [432]: s[2:5]
Out[432]: 
c    1.331458
d   -0.571329
e   -0.026671

However, if you only had c and e, determining the next element in the index can be somewhat complicated. For example, the following does not work:

s.ix['c':'e'+1]

A very common use case is to limit a time series to start and end at two specific dates. To enable this, we made the design design to make label-based slicing include both endpoints:

In [433]: s.ix['c':'e']
Out[433]: 
c    1.331458
d   -0.571329
e   -0.026671

This is most definitely a “practicality beats purity” sort of thing, but it is something to watch out for if you expect label-based slicing to behave exactly in the way that standard Python integer slicing works.

Miscellaneous indexing gotchas

Reindex versus ix gotchas

Many users will find themselves using the ix indexing capabilities as a concise means of selecting data from a pandas object:

In [434]: df = DataFrame(randn(6, 4), columns=['one', 'two', 'three', 'four'],
   .....:                index=list('abcdef'))
   .....:

In [435]: df
Out[435]: 
        one       two     three      four
a -1.114738 -0.058216 -0.486768  1.685148
b  0.112572 -1.495309  0.898435 -0.148217
c -1.596070  0.159653  0.262136  0.036220
d  0.184735 -0.255069 -0.271020  1.288393
e  0.294633 -1.165787  0.846974 -0.685597
f  0.609099 -0.303961  0.625555 -0.059268

In [436]: df.ix[['b', 'c', 'e']]
Out[436]: 
        one       two     three      four
b  0.112572 -1.495309  0.898435 -0.148217
c -1.596070  0.159653  0.262136  0.036220
e  0.294633 -1.165787  0.846974 -0.685597

This is, of course, completely equivalent in this case to using th reindex method:

In [437]: df.reindex(['b', 'c', 'e'])
Out[437]: 
        one       two     three      four
b  0.112572 -1.495309  0.898435 -0.148217
c -1.596070  0.159653  0.262136  0.036220
e  0.294633 -1.165787  0.846974 -0.685597

Some might conclude that ix and reindex are 100% equivalent based on this. This is indeed true except in the case of integer indexing. For example, the above operation could alternately have been expressed as:

In [438]: df.ix[[1, 2, 4]]
Out[438]: 
        one       two     three      four
b  0.112572 -1.495309  0.898435 -0.148217
c -1.596070  0.159653  0.262136  0.036220
e  0.294633 -1.165787  0.846974 -0.685597

If you pass [1, 2, 4] to reindex you will get another thing entirely:

In [439]: df.reindex([1, 2, 4])
Out[439]: 
   one  two  three  four
1  NaN  NaN    NaN   NaN
2  NaN  NaN    NaN   NaN
4  NaN  NaN    NaN   NaN

So it’s important to remember that reindex is strict label indexing only. This can lead to some potentially surprising results in pathological cases where an index contains, say, both integers and strings:

In [440]: s = Series([1, 2, 3], index=['a', 0, 1])

In [441]: s
Out[441]: 
a    1
0    2
1    3

In [442]: s.ix[[0, 1]]
Out[442]: 
0    2
1    3

In [443]: s.reindex([0, 1])
Out[443]: 
0    2
1    3

Because the index in this case does not contain solely integers, ix falls back on integer indexing. By contrast, reindex only looks for the values passed in the index, thus finding the integers 0 and 1. While it would be possible to insert some logic to check whether a passed sequence is all contained in the index, that logic would exact a very high cost in large data sets.

Timestamp limitations

Minimum and maximum timestamps

Since pandas represents timestamps in nanosecond resolution, the timespan that can be represented using a 64-bit integer is limited to approximately 584 years:

In [444]: begin = Timestamp(-9223285636854775809L)

In [445]: begin
Out[445]: <Timestamp: 1677-09-22 00:12:43.145224191>

In [446]: end = Timestamp(np.iinfo(np.int64).max)

In [447]: end
Out[447]: <Timestamp: 2262-04-11 23:47:16.854775807>

If you need to represent time series data outside the nanosecond timespan, use PeriodIndex:

In [448]: span = period_range('1215-01-01', '1381-01-01', freq='D')

In [449]: span
Out[449]: 
<class 'pandas.tseries.period.PeriodIndex'>
freq: D
[1215-01-01, ..., 1381-01-01]
length: 60632

Parsing Dates from Text Files

When parsing multiple text file columns into a single date column, the new date column is prepended to the data and then index_col specification is indexed off of the new set of columns rather than the original ones:

In [450]: print open('tmp.csv').read()
KORD,19990127, 19:00:00, 18:56:00, 0.8100
KORD,19990127, 20:00:00, 19:56:00, 0.0100
KORD,19990127, 21:00:00, 20:56:00, -0.5900
KORD,19990127, 21:00:00, 21:18:00, -0.9900
KORD,19990127, 22:00:00, 21:56:00, -0.5900
KORD,19990127, 23:00:00, 22:56:00, -0.5900

In [451]: date_spec = {'nominal': [1, 2], 'actual': [1, 3]}

In [452]: df = read_csv('tmp.csv', header=None,
   .....:               parse_dates=date_spec,
   .....:               keep_date_col=True,
   .....:               index_col=0)
   .....:

# index_col=0 refers to the combined column "nominal" and not the original
# first column of 'KORD' strings
In [453]: df
Out[453]: 
                                  actual    X0        X1         X2         X3    X4
nominal                                                                             
1999-01-27 19:00:00  1999-01-27 18:56:00  KORD  19990127   19:00:00   18:56:00  0.81
1999-01-27 20:00:00  1999-01-27 19:56:00  KORD  19990127   20:00:00   19:56:00  0.01
1999-01-27 21:00:00  1999-01-27 20:56:00  KORD  19990127   21:00:00   20:56:00 -0.59
1999-01-27 21:00:00  1999-01-27 21:18:00  KORD  19990127   21:00:00   21:18:00 -0.99
1999-01-27 22:00:00  1999-01-27 21:56:00  KORD  19990127   22:00:00   21:56:00 -0.59
1999-01-27 23:00:00  1999-01-27 22:56:00  KORD  19990127   23:00:00   22:56:00 -0.59

Differences with NumPy

For Series and DataFrame objects, var normalizes by N-1 to produce unbiased estimates of the sample variance, while NumPy’s var normalizes by N, which measures the variance of the sample. Note that cov normalizes by N-1 in both pandas and NumPy.