Skip to content

Commit 3718ec9

Browse files
committed
Fix bugs related to NaN values
1 parent 835d854 commit 3718ec9

2 files changed

Lines changed: 58 additions & 10 deletions

File tree

dabest/_dabest_object.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ def __init__(
5757
self._check_errors(x, y, idx, experiment, experiment_label, x1_level)
5858

5959

60+
# Check if there is NaN under any of the paired settings
61+
if self.__is_paired and self.__output_data.isnull().values.any():
62+
import warnings
63+
warn1 = f"NaN values detected under paired setting and removed,"
64+
warn2 = f" please check your data."
65+
warnings.warn(warn1 + warn2)
66+
if x is not None and y is not None:
67+
rmname = self.__output_data[self.__output_data[y].isnull()][self.__id_col].tolist()
68+
self.__output_data = self.__output_data[~self.__output_data[self.__id_col].isin(rmname)]
69+
elif x is None and y is None:
70+
self.__output_data.dropna(inplace=True)
71+
6072
# create new x & idx and record the second variable if this is a valid 2x2 ANOVA case
6173
if idx is None and x is not None and y is not None:
6274
# Add a length check for unique values in the first element in list x,
@@ -562,7 +574,16 @@ def _get_plot_data(self, x, y, all_plot_groups):
562574
"""
563575
Function to prepare some attributes for plotting
564576
"""
565-
577+
# Check if there is NaN under any of the paired settings
578+
if self.__is_paired is not None and self.__output_data.isnull().values.any():
579+
print("Nan")
580+
import warnings
581+
warn1 = f"NaN values detected under paired setting and removed,"
582+
warn2 = f" please check your data."
583+
warnings.warn(warn1 + warn2)
584+
rmname = self.__output_data[self.__output_data[y].isnull()][self.__id_col].tolist()
585+
self.__output_data = self.__output_data[~self.__output_data[self.__id_col].isin(rmname)]
586+
566587
# Identify the type of data that was passed in.
567588
if x is not None and y is not None:
568589
# Assume we have a long dataset.
@@ -606,6 +627,13 @@ def _get_plot_data(self, x, y, all_plot_groups):
606627
self.__xvar = "group"
607628
self.__yvar = "value"
608629

630+
# Check if there is NaN under any of the paired settings
631+
if self.__is_paired is not None and self.__output_data.isnull().values.any():
632+
import warnings
633+
warn1 = f"NaN values detected under paired setting and removed,"
634+
warn2 = f" please check your data."
635+
warnings.warn(warn1 + warn2)
636+
609637
# First, check we have all columns in the dataset.
610638
for g in all_plot_groups:
611639
if g not in self.__output_data.columns:
@@ -625,10 +653,6 @@ def _get_plot_data(self, x, y, all_plot_groups):
625653
var_name=self.__xvar,
626654
)
627655

628-
if self.__is_paired is not None and plot_data.isnull().values.any():
629-
import warnings
630-
warnings.warn("NaN values detected in paired setting, please remove NaNs and corresponding rows before proceeding.")
631-
632656
# Added in v0.2.7.
633657
plot_data.dropna(axis=0, how="any", subset=[self.__yvar], inplace=True)
634658

nbs/API/dabest_object.ipynb

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@
127127
" self._check_errors(x, y, idx, experiment, experiment_label, x1_level)\n",
128128
" \n",
129129
"\n",
130+
" # Check if there is NaN under any of the paired settings\n",
131+
" if self.__is_paired and self.__output_data.isnull().values.any():\n",
132+
" import warnings\n",
133+
" warn1 = f\"NaN values detected under paired setting and removed,\"\n",
134+
" warn2 = f\" please check your data.\"\n",
135+
" warnings.warn(warn1 + warn2)\n",
136+
" if x is not None and y is not None:\n",
137+
" rmname = self.__output_data[self.__output_data[y].isnull()][self.__id_col].tolist()\n",
138+
" self.__output_data = self.__output_data[~self.__output_data[self.__id_col].isin(rmname)]\n",
139+
" elif x is None and y is None:\n",
140+
" self.__output_data.dropna(inplace=True)\n",
141+
"\n",
130142
" # create new x & idx and record the second variable if this is a valid 2x2 ANOVA case\n",
131143
" if idx is None and x is not None and y is not None:\n",
132144
" # Add a length check for unique values in the first element in list x,\n",
@@ -632,7 +644,16 @@
632644
" \"\"\"\n",
633645
" Function to prepare some attributes for plotting\n",
634646
" \"\"\"\n",
635-
"\n",
647+
" # Check if there is NaN under any of the paired settings\n",
648+
" if self.__is_paired is not None and self.__output_data.isnull().values.any():\n",
649+
" print(\"Nan\")\n",
650+
" import warnings\n",
651+
" warn1 = f\"NaN values detected under paired setting and removed,\"\n",
652+
" warn2 = f\" please check your data.\"\n",
653+
" warnings.warn(warn1 + warn2)\n",
654+
" rmname = self.__output_data[self.__output_data[y].isnull()][self.__id_col].tolist()\n",
655+
" self.__output_data = self.__output_data[~self.__output_data[self.__id_col].isin(rmname)]\n",
656+
" \n",
636657
" # Identify the type of data that was passed in.\n",
637658
" if x is not None and y is not None:\n",
638659
" # Assume we have a long dataset.\n",
@@ -676,6 +697,13 @@
676697
" self.__xvar = \"group\"\n",
677698
" self.__yvar = \"value\"\n",
678699
"\n",
700+
" # Check if there is NaN under any of the paired settings\n",
701+
" if self.__is_paired is not None and self.__output_data.isnull().values.any():\n",
702+
" import warnings\n",
703+
" warn1 = f\"NaN values detected under paired setting and removed,\"\n",
704+
" warn2 = f\" please check your data.\"\n",
705+
" warnings.warn(warn1 + warn2)\n",
706+
"\n",
679707
" # First, check we have all columns in the dataset.\n",
680708
" for g in all_plot_groups:\n",
681709
" if g not in self.__output_data.columns:\n",
@@ -695,10 +723,6 @@
695723
" var_name=self.__xvar,\n",
696724
" )\n",
697725
"\n",
698-
" if self.__is_paired is not None and plot_data.isnull().values.any():\n",
699-
" import warnings\n",
700-
" warnings.warn(\"NaN values detected in paired setting, please remove NaNs and corresponding rows before proceeding.\")\n",
701-
"\n",
702726
" # Added in v0.2.7.\n",
703727
" plot_data.dropna(axis=0, how=\"any\", subset=[self.__yvar], inplace=True)\n",
704728
"\n",

0 commit comments

Comments
 (0)