# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
import numpy as np
"""
## What we'll look into?

- Analysis
    - what part of the day I listen the most?
    - What part of the year I listened the most?
    - The top artists
    - The top tracks

"""
#importing data
data = pd.read_json('../input/myspotifydata/StreamingHistory0.json')
data.head()
data.shape
#creating a column with the total minute played
data['minPlayed'] = data.msPlayed / 60000
data.drop(columns = 'msPlayed',  inplace = True)
#total number of minutes listened in hours
data.minPlayed.sum()/60
data.head()
#top 15 artists
top10_artists = data.artistName.value_counts()[:15]
print(top10_artists)
#top 15 tracks
top10_tracks = data.trackName.value_counts()[:15]
print(top10_tracks)
#converting endTime to datetime
data['endTime'] = pd.to_datetime(data['endTime'])
#creating a column for date, weekday and hour only
data['date'] = data['endTime'].dt.date
data['hour'] = data['endTime'].dt.hour
#data['weekday'] = pd.to_datetime(data['endTime'])
data['weekday'] = data['endTime'].dt.weekday
#checking for data types
data.dtypes
#taking a look at distribuition of listening in dates
plt.figure(figsize = (8,8))
plt.hist(data = data, x = 'date', bins = 12)
plt.xticks(rotation = 90)
plt.grid(True, axis = 'y')
plt.title('Distribution of music listened in 2020')
#filtering and creating another df 
days_hours = data.filter(['hour','weekday','minPlayed'])
#checking the process
days_hours
#making a plot with most common listening hours
plt.figure(figsize = (8,8))
col = sb.color_palette()[0]
#order = ['0h','1h','2h','3h','4h','5h','6h','7h','8h','9h','10h','11h','12h','13h','14h','15h','16h','17h','18h','19h','20h','21h','22h','23h']
sb.countplot(data = days_hours, x ='hour', color = col) #,  order = order)
plt.title('Most common listening hours')
plt.ylabel('Number of streams')
plt.xlabel('Hour')
plt.xticks(rotation = 90);
#making a plot with the most commom listening days
plt.figure(figsize = (8,8))
col = sb.color_palette()[0]
#order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
day_short_names = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']
sb.countplot(data = days_hours, x ='weekday', color = col) #order = order)
plt.xticks([0,1,2,3,4,5,6], day_short_names);
plt.title('Most common listening days')
plt.ylabel('Number of streams')
plt.xlabel('Weekday');
data['date']= pd.to_datetime(data['date'])
#pivoting the df and creating another one. We'll use wekkday and hour as index, because this is what
#we want to display in the heatmap
data_df = pd.pivot_table(data[['hour','weekday','minPlayed']], index = ['weekday','hour'], aggfunc = 'count')
#unstacking the data, and filling the NaN values with 0
data_df2 = data_df.unstack(level = 0)
data_df3 = data_df2.fillna(0)
data_df3
#plotting the heatmap with most listening hours and days
day_short_names = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']
plt.figure(figsize = (20,15))
sb.heatmap(data = data_df3, annot = True, cmap = 'BuGn', fmt = '000', xticklabels=day_short_names)
plt.title('Days and Hours with most listening to Spotify', fontsize = 25)
plt.xlabel('')
plt.ylabel('Hours - 24h format');
"""
- The day I listened the most was Wednesday
- My moments of most listening were during the night
- The most frequent time of listening is between 12h to 22h.
"""
"""
### Sources

[Heatmap](https://dfrieds.com/data-visualizations/when-use-heatmaps.html)

[Seaborn Documentation](https://seaborn.pydata.org/api.html)

[Changing xticks label](https://www.kite.com/python/answers/how-to-set-tick-labels-with-matplotlib-in-python)

[Matplotlib Documentation](https://matplotlib.org/3.3.3/index.html)

[Pandas Documentation](https://pandas.pydata.org/pandas-docs/stable/index.html)

[Color Palette](https://chrisalbon.com/python/data_visualization/seaborn_color_palettes/)
"""