Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting and Slicing Columns Which are a PeriodIndex

Tags:

python

pandas

I have a DataFrame where the columns are a PeriodIndex by Month as follows:

df = pd.DataFrame(np.random.randn(3,4), index = np.arange(3), columns = pd.period_range('2015-01', freq = 'M', periods = 4))


     2015-01     2015-02    2015-03      2015-04
0   -1.459943   -1.572013   2.977714    -0.067696
1   -1.545259   -0.570757   0.133756    -1.231192
2    0.498197   -0.555625   0.174545     0.371475

I can select a subset of columns as follows:

testdf[[pd.Period('2015-01'),pd.Period('2015-03')]]

     2015-01    2015-03
0   -1.459943   2.977714
1   -1.545259   0.133756
2    0.498197   0.174545

However when it comes to slicing to get for example all months from '2015-01' to '2015-03' I am stumped as to the syntax required. I have tried all kinds of iterations without luck.

For example:

df[pd.Period('2015-01'):pd.Period('2015-03')]
df['2015-01':'2015-03']

All of which do not work.

How can I slice this PeriodIndex?

like image 793
D. Pank Avatar asked Mar 18 '16 11:03

D. Pank


People also ask

What is slicing a DataFrame?

In Pandas, data is typically arranged in rows and columns. A DataFrame is an indexed and typed two-dimensional data structure. In Pandas, you can use a technique called DataFrame slicing to extract just the data you need from large or small datasets.

How do you select columns in a data frame?

Use DataFrame. loc[] and DataFrame. iloc[] to select a single column or multiple columns from pandas DataFrame by column names/label or index position respectively. where loc[] is used with column labels/names and iloc[] is used with column index/position.

How do I select a column for an index?

If you'd like to select columns based on integer indexing, you can use the . iloc function. If you'd like to select columns based on label indexing, you can use the . loc function.


1 Answers

Use .ix to pass a slice for the column selection arg:

In [9]:
df.ix[:,pd.Period('2015-01'):pd.Period('2015-03')]

Out[9]:
    2015-01   2015-02   2015-03
0  0.046028 -0.298445  0.908185
1 -0.955049 -1.420290  1.632564
2  0.848906  0.089553  0.551265
like image 194
EdChum Avatar answered Oct 04 '22 17:10

EdChum