code: def posthoc_tamhane(
      a: Union[list, np.ndarray, DataFrame],
      val_col: str = None,
      group_col: str = None,
      welch: bool = True,
      sort: bool = False,
  ) -> DataFrame:
      """Tamhane's T2 all-pairs comparison test for normally distributed data with
      unequal variances. Tamhane's T2 test can be performed for all-pairs
      comparisons in an one-factorial layout with normally distributed residuals
      but unequal groups variances. A total of m = k(k-1)/2 hypotheses can be
      tested. The null hypothesis is tested in the two-tailed test against the
      alternative hypothesis [1]_.
  
      Parameters
      ----------
      a : Union[list, np.ndarray, DataFrame]
          An array, any object exposing the array interface or a pandas
          DataFrame.
  
      val_col : str, optional
          Name of a DataFrame column that contains dependent variable values (test
          or response variable). Values should have a non-nominal scale. Must be
          specified if `a` is a pandas DataFrame object.
  
      group_col : str, optional
          Name of a DataFrame column that contains independent variable values
          (grouping or predictor variable). Values should have a nominal scale
          (categorical). Must be specified if `a` is a pandas DataFrame object.
  
      welch : bool, optional
          If True, use Welch's approximate solution for calculating the degree of
          freedom. T2 test uses the usual df = N - 2 approximation.
  
      sort : bool, optional
          If True, sort data by block and group columns.
  
      Returns
      -------
      result : pandas.DataFrame
          P values.
  
      Notes
      -----
      The p values are computed from the t-distribution and adjusted according to
      Dunn-Sidak.
  
      References
      ----------
      .. [1] A.C. Tamhane (1979), A Comparison of Procedures for Multiple Comparisons of
          Means with Unequal Variances. Journal of the American Statistical Association,
          74, 471-480.
  
      Examples
      --------
      >>> import scikit_posthocs as sp
      >>> import pandas as pd
      >>> x = pd.DataFrame({"a": [1,2,3,5,1], "b": [12,31,54,62,12], "c": [10,12,6,74,11]})
      >>> x = x.melt(var_name='groups', value_name='values')
      >>> sp.posthoc_tamhane(x, val_col='values', group_col='groups')
      """
      x, _val_col, _group_col = __convert_to_df(a, val_col, group_col)
      x = x.sort_values(by=[_group_col], ascending=True) if sort else x
  
      groups = x[_group_col].unique()
      x_grouped = x.groupby(_group_col)[_val_col]
      ni = x_grouped.count()
      xi = x_grouped.mean()
      si = x_grouped.var()
  
      def compare(i, j):
          dif = xi[i] - xi[j]
          A = si[i] / ni[i] + si[j] / ni[j]
          t_val = dif / np.sqrt(A)
          if welch:
              df = A**2.0 / (
                  si[i] ** 2.0 / (ni[i] ** 2.0 * (ni[i] - 1.0))
                  + si[j] ** 2.0 / (ni[j] ** 2.0 * (ni[j] - 1.0))
              )
          else:
              # checks according to Tamhane (1979, p. 474)
              ok1 = (9.0 / 10.0 <= ni[i] / ni[j]) and (
                  ni[i] / ni[j] <= 10.0 / 9.0
              )
              ok2 = (
                  9.0 / 10.0 <= (si[i] / ni[i]) / (si[j] / ni[j])
              ) and ((si[i] / ni[i]) / (si[j] / ni[j]) <= 10.0 / 9.0)
              ok3 = (
                  (4.0 / 5.0 <= ni[i] / ni[j])
                  and (ni[i] / ni[j] <= 5.0 / 4.0)
                  and (1.0 / 2.0 <= (si[i] / ni[i]) / (si[j] / ni[j]))
                  and ((si[i] / ni[i]) / (si[j] / ni[j]) <= 2.0)
              )
              ok4 = (
                  (2.0 / 3.0 <= ni[i] / ni[j])
                  and (ni[i] / ni[j] <= 3.0 / 2.0)
                  and (3.0 / 4.0 <= (si[i] / ni[i]) / (si[j] / ni[j]))
                  and ((si[i] / ni[i]) / (si[j] / ni[j]) <= 4.0 / 3.0)
              )
              OK = any([ok1, ok2, ok3, ok4])
              if not OK:
                  print(
                      "Sample sizes or standard errors are not balanced. T2 test is recommended."
                  )
              df = ni[i] + ni[j] - 2.0
          p_val = 2.0 * ss.t.sf(np.abs(t_val), df=df)
          return p_val
  
      vs = np.zeros((groups.size, groups.size), dtype=float)
      tri_upper = np.triu_indices(vs.shape[0], 1)
      tri_lower = np.tril_indices(vs.shape[0], -1)
      vs[:, :] = 0
  
      combs = it.combinations(range(groups.size), 2)
  
      for i, j in combs:
          vs[i, j] = compare(groups[i], groups[j])
  
      vs[tri_upper] = 1.0 - (1.0 - vs[tri_upper]) ** groups.size
      vs[tri_lower] = np.transpose(vs)[tri_lower]
      vs[vs > 1] = 1
  
      np.fill_diagonal(vs, 1)
      return DataFrame(vs, index=groups, columns=groups)
  
imports: import itertools
  from typing import Tuple, Union
  import numpy
  import scipy.stats
  from statsmodels.sandbox.stats.multicomp import multipletests
  from statsmodels.stats.multicomp import pairwise_tukeyhsd
  from statsmodels.stats.libqsturng import psturng
  from pandas import DataFrame
label_desc: Use image node_id 20 for calling a global function with example usage: posthoc_tamhane(a, val_col, group_col, welch, sort) and returns: DataFrame, p_val