I have a column that represents the number of days from an event until today.
JavaScript
x
2
1
daysSeries = pd.Series([2, 13,29,31,91,180,367,725],dtype=int64)
2
I am trying to figure out a way to represent this as a string such that it shows the rounded number of days / weeks / months / years. However, I would like it to choose “D”/”W”/”M”/”Y” based on the minimum value of each that is > 1. Some rounding is fine to make sure that a few days off doesnt mess it all up.
For example, if the input number is 31, I don’t want “4Weeks”, but instead “1Month”.
Using the example values about, I would expect:
JavaScript
1
2
1
["2Days", "2Weeks", "1Month", "1Month", "3Month", "6Months", "1Year", "2Year"].
2
Advertisement
Answer
I did it without the round, it’s much easier that way:
JavaScript
1
15
15
1
import pandas as pd
2
3
def lowest_str(days: int) -> str:
4
if days >= 365:
5
return f'{days // 365} Years'
6
if days >= 30:
7
return f'{days // 30} Months'
8
if days >= 7:
9
return f'{days // 7} Weeks'
10
return f'{days} Days'
11
12
13
daysSeries = pd.Series([2, 13, 29, 31, 91, 180, 367, 725])
14
print(daysSeries.apply(lowest_str))
15