I computed Pearson's chi-squared statistics for the contingency table bellow.
Here are the expected values.
Method 1 (using SciKit)
from scipy.stats import chi2_contingency
from scipy.stats import chi2
table = [[21,2], [14, 0]]
stat, p, dof, expected = chi2_contingency(table)
print(stat)
Returns
0.14814773735581194
Method 2 (directly)
G3 = 35*23/37
H3 = 2*23/37
G4 = 35*14/37
H4 = 2*14/37
(21-G3)**2/G3+(2-H3)**2/H3+(14-G4)**2/G4+(0-H4)**2/H4
Returns
1.2869565217391306
I expected the returned values to be equal. Why do they differ?


There are many slightly different implementations of chi-squared tests within and among various kinds of software?
Here are four tests in R, using your table:
Default chi-squared test in R, using Yates' continuity correction. It gives an error message because counts in some cells are too small for the chi-squared statistic to have approximately a chi-squared distribution.
Without the continuity correction: Another error message because of low counts.
Simulation to give a more reliable P-value:
Fisher exact test uses a hypergeometric distribution:
None of the tests gives a P-value significant at the 5% level. With only two subjects in 'Senzitvne` outcome, you have no chance for a significant between groups.