direl-ts-tool-kit 0.1.0__tar.gz → 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: direl-ts-tool-kit
3
- Version: 0.1.0
3
+ Version: 0.2.2
4
4
  Summary: A toolbox for time series analysis and visualization.
5
5
  Home-page: https://gitlab.com/direl/direl_tool_kit
6
- Author: Tu Nombre Aquí
6
+ Author: Diego Restrepo-Leal
7
7
  Author-email: diegorestrepoleal@gmail.com
8
8
  Classifier: Programming Language :: Python :: 3
9
9
  Classifier: Programming Language :: Python :: 3.9
@@ -17,6 +17,7 @@ License-File: LICENCE
17
17
  Requires-Dist: pandas>=1.0.0
18
18
  Requires-Dist: numpy>=1.18.0
19
19
  Requires-Dist: matplotlib>=3.0.0
20
+ Requires-Dist: openpyxl
20
21
  Dynamic: author
21
22
  Dynamic: author-email
22
23
  Dynamic: classifier
@@ -1,2 +1,3 @@
1
1
  from .plot.plot_style import *
2
2
  from .plot.plot_ts import *
3
+ from .utilities.data_prep import *
@@ -0,0 +1,2 @@
1
+ from .plot_style import *
2
+ from .plot_ts import *
@@ -0,0 +1,48 @@
1
+ import matplotlib.pyplot as plt
2
+ import matplotlib.dates as mdates
3
+
4
+ plt.style.use("fast")
5
+
6
+ plt.rc("figure", autolayout=True, figsize=(12, 5))
7
+
8
+ plt.rc(
9
+ "axes",
10
+ labelweight="bold",
11
+ labelsize=15,
12
+ titleweight="bold",
13
+ titlesize=14,
14
+ titlepad=10,
15
+ facecolor="white",
16
+ )
17
+
18
+ plt.rc("xtick", labelsize=15)
19
+ plt.rc("ytick", labelsize=15)
20
+ plt.rc("legend", fontsize=10)
21
+
22
+ plot_params = {
23
+ "color": "0.75",
24
+ "style": ".-",
25
+ "markeredgecolor": "0.25",
26
+ "markerfacecolor": "0.25",
27
+ "legend": False,
28
+ }
29
+
30
+
31
+ paper_colors = {
32
+ "BLUE_LINES": "#396AB1",
33
+ "ORANGE_LINES": "#DA7C30",
34
+ "GREEN_LINES": "#3E9651",
35
+ "RED_LINES": "#CC2529",
36
+ "GRAY_LINES": "#535154",
37
+ "PURPLE_LINES": "#6B4C9A",
38
+ "MAROON_LINES": "#922428",
39
+ "GOLD_LINES": "#948B3D",
40
+ "BLUE_BARS": "#7293CB",
41
+ "ORANGE_BARS": "#E1974C",
42
+ "GREEN_BARS": "#84BA5B",
43
+ "RED_BARS": "#D35E60",
44
+ "GRAY_BARS": "#808585",
45
+ "PURPLE_BARS": "#90679D",
46
+ "MAROON_BARS": "#AB6857",
47
+ "GOLD_BARS": "#CCC210",
48
+ }
@@ -0,0 +1,149 @@
1
+ from .plot_style import *
2
+
3
+
4
+ def plot_time_series(
5
+ df1, variable, units="", color="BLUE_LINES", time_unit="Year", rot=90, method=True
6
+ ):
7
+ """
8
+ Plots a time series with custom styling and dual-level grid visibility.
9
+
10
+ This function automatically sets major and minor time-based locators
11
+ on the x-axis based on the specified time unit, and formats the y-axis
12
+ to use scientific notation.
13
+
14
+ Parameters
15
+ ----------
16
+ df1 : pd.DataFrame
17
+ The DataFrame containing the time series data. Must have a DatetimeIndex.
18
+ variable : str
19
+ The name of the column to plot. The label is automatically formatted
20
+ (e.g., 'total_sales' becomes 'Total Sales').
21
+ units : str, optional
22
+ Units to display next to the variable name on the y-axis (e.g., 'USD').
23
+ Defaults to "".
24
+ color : str, optional
25
+ Key corresponding to the line color in the global 'paper_colors' dictionary.
26
+ Defaults to "BLUE_LINES".
27
+ time_unit : str, optional
28
+ The time granularity of the data to define x-axis tick locators.
29
+ Options include 'Year', 'Month', 'Weekday', or 'Day'. Defaults to "Year".
30
+ rot : int, optional
31
+ Rotation angle (in degrees) for the x-axis tick labels. Defaults to 90.
32
+ method : bool, optional
33
+ Used internally for label formatting logic. Defaults to True.
34
+
35
+ Returns
36
+ -------
37
+ matplotlib.figure.Figure
38
+ The generated matplotlib figure object.
39
+
40
+ Notes
41
+ -----
42
+ Major grid lines are displayed with a dashed line ('--'), and minor grid
43
+ lines are displayed with a dotted line (':') for detailed temporal analysis.
44
+
45
+ Available Colors
46
+ ----------------
47
+ The 'color' parameter accepts any key from the 'paper_colors' dictionary.
48
+
49
+ Lines: 'BLUE_LINES', 'ORANGE_LINES', 'GREEN_LINES', 'RED_LINES',
50
+ 'GRAY_LINES', 'PURPLE_LINES', 'MAROON_LINES', 'GOLD_LINES'.
51
+
52
+ Bars: 'BLUE_BARS', 'ORANGE_BARS', 'GREEN_BARS', 'RED_BARS',
53
+ 'GRAY_BARS', 'PURPLE_BARS', 'MAROON_BARS', 'GOLD_BARS'.
54
+ """
55
+
56
+ fig, ax = plt.subplots()
57
+ ax.plot(df1.index, df1[variable], linewidth=3, color=paper_colors[color])
58
+
59
+ if "-" in variable:
60
+ variable = "-".join(
61
+ [
62
+ j.title() if i == 0 else j.lower()
63
+ for i, j in enumerate(variable.split("-"))
64
+ ]
65
+ )
66
+ elif "_" in variable:
67
+ variable = " ".join(
68
+ [
69
+ j.title() if i == 0 else j.lower()
70
+ for i, j in enumerate(variable.split("_"))
71
+ ]
72
+ )
73
+ else:
74
+ variable = (
75
+ " ".join(
76
+ [
77
+ j.title() if i == 0 else j.lower()
78
+ for i, j in enumerate(variable.split())
79
+ ]
80
+ )
81
+ if method
82
+ else variable
83
+ )
84
+
85
+ ax.set(xlabel=f"{time_unit}", ylabel=f"{variable} {units}")
86
+ ax.ticklabel_format(style="sci", axis="y", scilimits=(0, 0))
87
+
88
+ if time_unit == "Year":
89
+ ax.xaxis.set_major_locator(mdates.YearLocator())
90
+ ax.xaxis.set_minor_locator(mdates.MonthLocator())
91
+
92
+ if time_unit == "Month":
93
+ ax.xaxis.set_major_locator(mdates.MonthLocator())
94
+ ax.xaxis.set_minor_locator(mdates.WeekdayLocator())
95
+
96
+ if time_unit == "Weekday":
97
+ ax.xaxis.set_major_locator(mdates.WeekdayLocator())
98
+ ax.xaxis.set_minor_locator(mdates.DayLocator())
99
+
100
+ if time_unit == "Day":
101
+ ax.xaxis.set_major_locator(mdates.DayLocator())
102
+ ax.xaxis.set_minor_locator(mdates.HourLocator())
103
+
104
+ ax.tick_params(axis="x", rotation=rot)
105
+ ax.grid(which="both")
106
+ ax.grid(which="minor", alpha=0.6, linestyle=":")
107
+ ax.grid(which="major", alpha=0.8, linestyle="--")
108
+
109
+ return fig
110
+
111
+
112
+ def save_figure(
113
+ fig,
114
+ file_name,
115
+ variable_name="",
116
+ path="./",
117
+ ):
118
+ """
119
+ Saves a Matplotlib figure in three common high-quality formats (PNG, PDF, SVG).
120
+
121
+ The function creates a consistent file name structure:
122
+ {path}/{file_name}_{variable_name}.{extension}.
123
+
124
+ Parameters
125
+ ----------
126
+ fig : matplotlib.figure.Figure
127
+ The Matplotlib figure object to be saved.
128
+ file_name : str
129
+ The primary name for the file (e.g., 'timeseries_report').
130
+ variable_name : str, optional
131
+ An optional secondary name, often the name of the plotted variable,
132
+ to be appended to the file name. Defaults to "".
133
+ path : str, optional
134
+ The directory path where the figure files will be saved.
135
+ Defaults to the current directory ('./').
136
+
137
+ Returns
138
+ -------
139
+ None
140
+ """
141
+
142
+ if variable_name:
143
+ base_name = f"{path}/{file_name}_{variable_name}"
144
+ else:
145
+ base_name = f"{path}/{file_name}"
146
+
147
+ fig.savefig(f"{base_name}.png")
148
+ fig.savefig(f"{base_name}.pdf")
149
+ fig.savefig(f"{base_name}.svg")
@@ -0,0 +1 @@
1
+ from .data_prep import *
@@ -0,0 +1,35 @@
1
+ import pandas as pd
2
+
3
+
4
+ def parse_datetime_index(df_raw, date_column="date"):
5
+ """
6
+ Parses a specified column into datetime objects and sets it as the DataFrame index.
7
+
8
+ This function is crucial for preparing raw data (df_raw) for time series analysis
9
+ by ensuring the DataFrame is indexed by the correct datetime type.
10
+
11
+ Parameters
12
+ ----------
13
+ df_raw : pd.DataFrame
14
+ The raw DataFrame containing the data, including the column with date strings.
15
+ date_column : str, optional
16
+ The name of the column in 'df_raw' that contains the date/time information.
17
+ Defaults to "date".
18
+
19
+ Returns
20
+ -------
21
+ df_ts : pd.DataFrame
22
+ A copy of the original DataFrame with the specified date column removed
23
+ and set as the DatetimeIndex. Ready for time series plotting.
24
+ original_dates : pd.Series
25
+ The original Series containing the date strings/objects, which was used
26
+ to create the new index.
27
+ """
28
+
29
+ date_parsed = pd.to_datetime(df_raw[date_column])
30
+ df_ts = df_raw.copy()
31
+ original_dates = df_raw[date_column]
32
+ df_ts.drop(columns=[date_column], inplace=True)
33
+ df_ts.set_index(date_parsed, inplace=True)
34
+
35
+ return df_ts, original_dates
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: direl-ts-tool-kit
3
- Version: 0.1.0
3
+ Version: 0.2.2
4
4
  Summary: A toolbox for time series analysis and visualization.
5
5
  Home-page: https://gitlab.com/direl/direl_tool_kit
6
- Author: Tu Nombre Aquí
6
+ Author: Diego Restrepo-Leal
7
7
  Author-email: diegorestrepoleal@gmail.com
8
8
  Classifier: Programming Language :: Python :: 3
9
9
  Classifier: Programming Language :: Python :: 3.9
@@ -17,6 +17,7 @@ License-File: LICENCE
17
17
  Requires-Dist: pandas>=1.0.0
18
18
  Requires-Dist: numpy>=1.18.0
19
19
  Requires-Dist: matplotlib>=3.0.0
20
+ Requires-Dist: openpyxl
20
21
  Dynamic: author
21
22
  Dynamic: author-email
22
23
  Dynamic: classifier
@@ -0,0 +1,14 @@
1
+ LICENCE
2
+ README.md
3
+ setup.py
4
+ direl_ts_tool_kit/__init__.py
5
+ direl_ts_tool_kit.egg-info/PKG-INFO
6
+ direl_ts_tool_kit.egg-info/SOURCES.txt
7
+ direl_ts_tool_kit.egg-info/dependency_links.txt
8
+ direl_ts_tool_kit.egg-info/requires.txt
9
+ direl_ts_tool_kit.egg-info/top_level.txt
10
+ direl_ts_tool_kit/plot/__init__.py
11
+ direl_ts_tool_kit/plot/plot_style.py
12
+ direl_ts_tool_kit/plot/plot_ts.py
13
+ direl_ts_tool_kit/utilities/__init__.py
14
+ direl_ts_tool_kit/utilities/data_prep.py
@@ -1,3 +1,4 @@
1
1
  pandas>=1.0.0
2
2
  numpy>=1.18.0
3
3
  matplotlib>=3.0.0
4
+ openpyxl
@@ -2,11 +2,11 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="direl-ts-tool-kit",
5
- version="0.1.0",
5
+ version="0.2.2",
6
6
  description="A toolbox for time series analysis and visualization.",
7
7
  long_description=open("README.md", encoding="utf-8").read(),
8
8
  long_description_content_type="text/markdown",
9
- author="Tu Nombre Aquí",
9
+ author="Diego Restrepo-Leal",
10
10
  author_email="diegorestrepoleal@gmail.com",
11
11
  url="https://gitlab.com/direl/direl_tool_kit",
12
12
  packages=find_packages(),
@@ -14,6 +14,7 @@ setup(
14
14
  "pandas>=1.0.0",
15
15
  "numpy>=1.18.0",
16
16
  "matplotlib>=3.0.0",
17
+ "openpyxl",
17
18
  ],
18
19
  classifiers=[
19
20
  "Programming Language :: Python :: 3",
@@ -1,9 +0,0 @@
1
- LICENCE
2
- README.md
3
- setup.py
4
- direl_ts_tool_kit/__init__.py
5
- direl_ts_tool_kit.egg-info/PKG-INFO
6
- direl_ts_tool_kit.egg-info/SOURCES.txt
7
- direl_ts_tool_kit.egg-info/dependency_links.txt
8
- direl_ts_tool_kit.egg-info/requires.txt
9
- direl_ts_tool_kit.egg-info/top_level.txt