Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated pandas.Series.iteritems #155

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/adtk/data/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ def validate_events(
merged_event_list = (
[]
) # type: List[Union[Tuple[pd.Timestamp, pd.Timestamp], pd.Timestamp]]
for t, v in time_window_end_series.iteritems(): # type: pd.Timestamp, int
# @modified 20240113 - earthgecko/adtk - Task #5224: Replace deprecated pandas.Series.iteritems
# earthgecko/skyline - Bug #5222: adtk - pandas error
# arundo/adtk - https://github.com/arundo/adtk/issues/147
# pandas.Series.iteritems - https://pandas.pydata.org/pandas-docs/version/1.5/reference/api/pandas.Series.iteritems.html
# Deprecated since version 1.5.0: iteritems is deprecated and will be removed in a future version. Use .items instead.
# for t, v in time_window_end_series.iteritems(): # type: pd.Timestamp, int
for t, v in time_window_end_series.items(): # type: pd.Timestamp, int
if (status == 0) and (v > 0):
start = t # type: pd.Timestamp
status = 1
Expand Down Expand Up @@ -588,7 +594,13 @@ def expand_events( # type:ignore
right_expand=right_expand,
freq_as_period=freq_as_period,
)
for _, s in events.iteritems()
# @modified 20240113 - earthgecko/adtk - Task #5224: Replace deprecated pandas.Series.iteritems
# earthgecko/skyline - Bug #5222: adtk - pandas error
# arundo/adtk - https://github.com/arundo/adtk/issues/147
# pandas.Series.iteritems - https://pandas.pydata.org/pandas-docs/version/1.5/reference/api/pandas.Series.iteritems.html
# Deprecated since version 1.5.0: iteritems is deprecated and will be removed in a future version. Use .items instead.
# for _, s in events.iteritems()
for _, s in events.iter()
],
axis=1,
) # type: pd.DataFrame
Expand Down
46 changes: 46 additions & 0 deletions src/adtk/visualization/_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
We don't typing the visualization module because there are a lot recursion on
nested tree structure which would be messy if we type rigorously."""

import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -31,6 +32,8 @@ def plot(
axes=None,
figsize=None,
legend=True,
title="",
save_to_file="",
):
"""Plot time series and/or anomalies.

Expand Down Expand Up @@ -191,6 +194,14 @@ def plot(
legend: bool, optional
Whether to show legend in the plot. Default: True.

title: str, optional
The title add to the plot.
Default: "".

save_to_file: str, optional
The full path and filename where to save the plot as a png image.
Default: "".

Returns
--------
matplotlib Axes object or array of Axes objects
Expand Down Expand Up @@ -329,6 +340,41 @@ def plot(
for ax in axes:
ax.legend()

# title
if title != "":
try:
plt.suptitle(title, fontsize=12)
except Exception as e:
print("could not add title to plot - {}".format(e))

# save_to_file
save_to_file_path = None
if save_to_file != "":
try:
save_to_file_path = os.path.dirname(save_to_file)
except Exception as e:
print(
"{} not a valid path/filename for "
"save_to_file - %s".format(save_to_file, e))

save_to_file_path_exists = False
if save_to_file_path:
try:
if os.path.exists(save_to_file_path):
save_to_file_path_exists = True
except Exception as e:
print(
"{} is not a valid path in "
"save_to_file - {}".format(save_to_file, e))
if save_to_file_path_exists:
try:
plt.savefig(save_to_file, dpi=100)
os.chmod(save_to_file, mode=0o644)
except Exception as e:
print(
"error: failed to save plot to "
"%s - %s".format(save_to_file, e))

return axes


Expand Down