1

I have a data that looks like this

data = [(u'Del', datetime.datetime(2019, 11, 1, 0, 0), 59L), (u'Bom', datetime.datetime(2019, 11, 1, 0, 0), 449L), (u'Del', datetime.datetime(2019, 12, 1, 0, 0), 0L), (u'Bom', datetime.datetime(2019, 12, 1, 0, 0), 45L)]

I am using pandas to do some data wrangling. Now I need to get a list of dates from the data in certain formats like hour, month, year etc. So to extract dates from the data in month, this is what I do

import pandas as pd
import datetime

df = pd.DataFrame(data)
dates = df[1].dt.month.values.tolist()

So I get a list of months. But in the method df[1].dt.year.values.tolist(), I want to assign the granularity dynamically ie it could either be dt.month or dt.year based on the variable value.

Say I have a variable called granularity_val

So if granularity_val = 'year', I should do

dates = df[1].dt.year.values.tolist()

Similarly if granularity_val = 'month', I should do

dates = df[1].dt.month.values.tolist()

So I tried doing something like this

granularity_val = 'year'
dates = df[1].dt.granularity_val.values.tolist()

But it throws an error like below

AttributeError: 'DatetimeProperties' object has no attribute 'granularity_val'

I understand it is trying to treat the variable as an attribute of dt object of pandas. So how do I make python understand that it needs to treat granularity_val not as an attribute but as a variable value?

4 Answers 4

3

You can use getattr to reference any attribute by string

getattr(df[1].dt, granularity_val).values.tolist()
Sign up to request clarification or add additional context in comments.

Comments

2

Use getattr for get attribute and for pandas 0.24+ instead values use Series.to_numpy:

df = pd.DataFrame({1: pd.date_range('2010-01-02', periods=5)})

granularity_val = 'month'
dates = getattr(df[1].dt, granularity_val).to_numpy().tolist()
print (dates)
[1, 1, 1, 1, 1]

granularity_val = 'year'
dates = getattr(df[1].dt, granularity_val).to_numpy().tolist()
print (dates)
[2010, 2010, 2010, 2010, 2010]

1 Comment

@SouvikRay so your pandas version is under 0.24+
1

Here's one way to do using strftime:

granularity_val = 'year'

match = {'year': '%Y', 'month': '%m', 'day': '%d'}

date = df.date.dt.strftime(match[granularity_val])

print(date)
['01', '01', '01', '01', '01']

Data

df = pd.DataFrame({'date': pd.date_range(start='2019-01-01', freq='D', periods=5))

Comments

0

For an example, let's use python's turtle.

import turtle 
t = turtle.Turtle();
t.color('red','blue');

So this sets our turtle to the color red, and it's border to blue. Now, let's say later on in the code we would want turtle's color. We can use this code.

print({t.color()});

In turtle, this will give us a very odd set: set([('red','blue')]) But, in most other cases, this will give us better feedback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.