1

I have timestamps in pandas and I want to be able to write a function that allows me to pull back different parts of the date. Here is an MWE:

import pandas as pd
df = pd.DataFrame({'hit': pd.to_datetime(['2018-12-29 00:00:00', '2019-03-25 00:00:00'])})

And this is what I want to be able to do:

def test(timepart):
    df['hit'].dt.timepart

So I could call test(month) and the function would return 12, 03.

ALollz
  • 57,915
  • 7
  • 66
  • 89
bshelt141
  • 1,183
  • 15
  • 31

2 Answers2

4

Use getattr to return that particular attribute:

def test(df, timepart):
    return getattr(df['hit'].dt, timepart)

test(df, 'month')
0    12
1     3
Name: hit, dtype: int64

The issue with strftime is that it converts the result to a string before returning it.


If you want a taste of the dynamic life, try DataFrame.eval instead.

def test(df, timepart):
    return df.eval(f"hit.dt.{timepart}", engine='python')

test(df, 'month')

0    12
1     3
Name: hit, dtype: int64

python's eval is dangerous, thankfully there are safer alternatives to do the same thing.

cs95
  • 379,657
  • 97
  • 704
  • 746
2

You can use strftime:

def test(timepart):
    return df['hit'].dt.strftime('%'+timepart[0])
print(test('month'))

Output:

0    12
1    03
Name: hit, dtype: object
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • thanks. i implemented the first `eval` version into my real script by the time you edited your answer, and it is working as expected. Besides cleaner syntax, is there a reason you prefer `strftime` over `eval`? – bshelt141 Jul 02 '19 at 03:16
  • 1
    @bshelt141 https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice – U13-Forward Jul 02 '19 at 03:17
  • 1
    @bshelt141 Please, please, please do not use `eval`...`.strftime` is OK if you want strings, but whatever it is, not `eval` please. Thanks. – cs95 Jul 02 '19 at 03:22