The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Music Box Puzzles

--

Chapters
00:00 Question
01:20 Accepted answer (Score 3)
01:50 Answer 2 (Score 2)
02:05 Answer 3 (Score 1)
02:31 Thank you

--

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

Answer 2 links:
[truncate]: https://pandas.pydata.org/pandas-docs/st...

--

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