The Python Oracle

How to select dataframe columns using string keys when the column names are timestamps?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Book End

--

Chapters
00:00 How To Select Dataframe Columns Using String Keys When The Column Names Are Timestamps?
01:06 Accepted Answer Score 3
01:26 Answer 2 Score 2
01:38 Answer 3 Score 1
01:55 Thank you

--

Full question
https://stackoverflow.com/questions/5029...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe #indexing #timestamp

#avk47



ACCEPTED ANSWER

Score 3


Well, there is always the filter option.

df = df.T
df.filter(like='2000-05')

   2000-05-31
A    1.884517
B    0.258133
C    0.809360
D   -0.069186

filter gives you greater flexibility, for example, with regular expressions:

df.filter(regex='2000-.*-30')

   2000-04-30  2000-06-30
A   -2.968870    2.064582
B   -0.844370    0.093393
C    0.027328    0.033193
D   -0.270860   -0.455323



ANSWER 2

Score 2


Maybe you can try str , contains

df[df.index.str.contains('2000-05')].T
Out[163]: 
   2000-05-31
A    0.639211
B   -0.209961
C   -1.006498
D    0.005214



ANSWER 3

Score 1


There's also truncate, which allows you to work with datetime objects instead of treating the column names as strings.

This would require two dates, though - before and after arguments act as bookends for the period you want to retain.

df_t = df.T
df_t.columns = pd.to_datetime(df_t.columns)

df_t.truncate(after="2000-03", before="2000-02", axis=1)

   2000-02-29
A    0.256296
B    1.838310
C    0.227132
D    1.765269