<jupyter_start><jupyter_text>Bought a BikeMy bike is 10 years old.  It cost `$100`, but I still use it today.  I wondered, **how profitably I invested** that `$100` in my bike?  Profitablity CalcI have prepared a simple calculator that can help to prevent spontaneous purchases.The calculator needs the following input parameters:- `purchase_name` - purchase name;- `price` - purchase price;- `usage_years` - how many years the purchase will be used;- `usage_seasons` - what seasons the purchase will be used, `winter`, `spring`, `summer`, `autumn` are allowed;- `usage_per_week` - how many times a week the purchase will be used;- `price_sale_pct` - how much the purchase was later sold for.<jupyter_code># Import libs
import pandas as pd
import calendar
import matplotlib.pyplot as plt
import seaborn as sns
def profitability(purchase_name, price, usage_years, usage_seasons, usage_per_week, price_sale_pct=0):
    # Calculate number of weeks in season
    days = 0
    seasons = list(set(usage_seasons))
    for season in seasons:
        if season == 'winter':
            days += calendar.monthrange(1970, 12)[1]
            days += calendar.monthrange(1970, 1)[1]
            days += calendar.monthrange(1970, 2)[1]
        elif season == 'spring':
            days += calendar.monthrange(1970, 3)[1]
            days += calendar.monthrange(1970, 4)[1]
            days += calendar.monthrange(1970, 5)[1]
        elif season == 'summer':
            days += calendar.monthrange(1970, 6)[1]
            days += calendar.monthrange(1970, 7)[1]
            days += calendar.monthrange(1970, 8)[1]
        elif season == 'autumn':
            days += calendar.monthrange(1970, 9)[1]
            days += calendar.monthrange(1970, 10)[1]
            days += calendar.monthrange(1970, 11)[1]
        else:
            return 'ERROR: not a season! Add winter, spring, summer or autumn.'
    usage_weeks = days/7

    # How many times have used the purchase
    usage_num = round(usage_years * usage_weeks * usage_per_week)
    # Сost per use
    price_per_usage = round((price - price_sale_pct) / usage_num, 2)
    
    result = pd.DataFrame({'purchase_name':[purchase_name],
                           'price':[price],
                           'usage_years':[usage_years],
                           'usage_seasons':[usage_seasons],
                           'usage_per_week':[usage_per_week],
                           'price_sale_pct':[price_sale_pct],
                           'usage_num':[usage_num],
                           'price_per_usage':[price_per_usage]
                        })

    return result<jupyter_output><empty_output><jupyter_text>ExampleLet's test the calculator to answer if my old bike is useful acquisition.<jupyter_code>profitability('my old bike', 100, 10, ['spring', 'summer'], 2)<jupyter_output><empty_output><jupyter_text>For 10 years now, I've been cycling twice a week in the spring and summer, and I "pay" 19 cents each time.It seems to be a bargain.One use can be understood as one hour of cycling or two hours, for example.Let's try to reduce the usage years to 1. Leave only summer for cycling.<jupyter_code>profitability('my old bike', 100, 1, ['summer'], 2)<jupyter_output><empty_output><jupyter_text>The cost of `$3.85` at a time (or an hour) is no longer so attractive.  Profitability to Buy a BikeRecently, I liked a new bike in the store.  I could have made a spontaneous, thoughtless purchase, given into the mood.  However, now I want to estimate how much the new bike will actually cost me. Let's do the calcs. Getting dataI will consider some of the uses for my new bike:- I'll buy it in the store for `$400`.- I'll use it 1, 2, 4, 5, 6, 7, 8, 9 or 10 years.- I plan to cycle only in summer.- I want to cycle, maybe once a week, or on weekends, or time to time, or on weekdays.- I'm not going to sell it.<jupyter_code># Describe the purchase
purchase_name_b = 'bike'
price_b = 400
usage_years_b = [y/2 for y in range(2, 21, 1)] # from 1 to 10 years
usage_seasons_b = [['summer']] # cycling only in summer
usage_per_week_b = [1,2,3,5] # once a week, on weekends, time to time, on weekdays
price_sale_pct_b = 0 # do not sell

columns = ['purchase_name', 'price', 'usage_years',
           'usage_seasons', 'usage_per_week', 'profitability'
          ]

profitability_bike = pd.DataFrame()
for years in usage_years_b:
    for seasons in usage_seasons_b:
        for times in usage_per_week_b:
            pra_bike = profitability(purchase_name_b, price_b, years, seasons, times, price_sale_pct_b)
            profitability_bike = profitability_bike.append(pra_bike)
            
profitability_bike.head()
profitability_bike.describe()
profitability_bike.info()
# Increase the number of rows to display
pd.set_option('display.max_rows', 100)

profitability_bike.sort_values(by='price_per_usage')<jupyter_output><empty_output><jupyter_text>Plotting price per usage<jupyter_code>%matplotlib inline

# Turn on svg rendering
%config InlineBackend.figure_format = 'svg'

# Color palette for the blog
snark_palette = ['#e0675a', # red
                 '#5ca0af', # green
                 '#edde7e', # yellow
                 '#211c47' # dark blue
                ]
# Color palette for the data
palette = 'tab10'

# Inscriptions
title = """Bike Profitability"""
description = """
The real cost of the bike depending on the frequency of use.
Data: @data.sugar | Author: @data.sugar
"""

# Plot size
figsize = (6,4)

# Set the figure
sns.set(context='paper', style='ticks', palette=snark_palette,
        rc={'axes.spines.left': True, 'axes.spines.bottom': True,
            'axes.spines.right': False, 'axes.spines.top': False
           }
       )

# Create the plot
fig, ax = plt.subplots(1, 1, figsize=figsize, facecolor='w')
sns.lineplot(data=profitability_bike,
             x='usage_years', y='price_per_usage',
             hue='usage_per_week',
             lw=2, palette=palette,
             legend='full', ax=ax
            )

# Set some aesthetic params for the plot
ax.set_title(title, fontdict={'fontsize': 16}, loc='center', pad=10, c=snark_palette[-1]) # set a title of the plot
ax.annotate(description, xy=(10, -4), size=6, xycoords='figure points', c=snark_palette[-1])
ax.set_xlabel('years of use', loc='center', size='x-large', c=snark_palette[-1]) # set label of x axis
ax.set_ylabel('$ per use', loc='center', size='x-large', c=snark_palette[-1]) # set label of y axis
ax.tick_params(axis='both', labelsize='large', colors=snark_palette[-1]) # set x/y ticks
ax.spines['bottom'].set_color(snark_palette[-1]) # color x axis
ax.spines['left'].set_color(snark_palette[-1]) # color y axis
ax.set_xticks([i for i in range(1, 11)]) # set x ticks labels
ax.set_xlim(1,10)
ax.set_ylim(0, 16)
ax.legend(loc='upper right', frameon=False).texts[0].set_text('')
ax.text(s='times used\nper week\nin summer', x=9.3, y=15, ha='center')

# Save and plot
plt.savefig('plot.pic/plot.bike.png', dpi=150, bbox_inches='tight')
plt.show()<jupyter_output><empty_output><jupyter_text>Do I need a bike?If renting a bike in my city costs `$1` an hour, then I have to cycle whole summer for at least 6 years to justify the purchase.  Blog Post<jupyter_code>blog_post = r"""
## 🚴‍♀️Do I need a bike?  


My \#bike is 10 years old.  
It cost \$100, but I still use it today.   

👀Thinking about \#conscious \#consumption I wondered how profitably I invested that \$100 in my bike?  

📌I have prepared a simple \#calculator to help prevent spontaneous purchases.  

I have tested it to answer if my old bike is \#useful acquisition.

✔For 10 years now, I've been cycling twice a week in the spring and summer, and I "pay" 19 cents each time.  
It seems to be a bargain.

✔What if reduce the usage years to 1 and leave only summer for cycling?  
The cost has increased to $3.85 at a time (or an hour) and is no longer so attractive.

✔One use can be understood as one hour of cycling or two hours, for example.

🤍Recently, I liked a new bike in the store.
I could have made a spontaneous, thoughtless purchase, given into the mood.
However, now I would like to estimate how much the new bike will actually cost me.  
So I did the calcs.

I considered some of the uses for my new bike:

◽I'll buy it in the store for \$400.  
◽I'll use it 1, 2, 4, 5, 6, 7, 8, 9 or 10 years.  
◽I plan to cycle only in summer.  
◽I want to cycle, maybe once a week, or on weekends, or time to time, or on weekdays.  
◽I'm not going to sell it.  

📝Do I still need a bike?  

If renting a bike in my city costs $1 an hour, then I have to cycle whole summer for at least 6 years to justify the purchase.  

(Need to calc your own purchases? Follow the link in bio for the profitability calc!)  
.  
.  
.  
\#funtime \#probably \#datascience \#datapower \#data_sugar_profit \#consciousconsumption
\#data_know_everything_and_nothing \#linkinbio \#datajournalism
"""

# Check post text length for Instagram
len(blog_post)
from IPython.display import Markdown as md
md(blog_post)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10753880266075388, 'fraction_numerical': 0.024611973392461198, 'mean_word_length': 3.383381924198251, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '320078', 'license': 'MIT', 'n_tokens_mistral': 3101, 'n_tokens_neox': 2863, 'n_words': 1037, 'path': '00003_conscious_consumption/conscious_consumption.bike.ipynb', 'repo_name': 'chupstee/data.sugar', 'size': '9020'}