It should be noted that the square wave signal has odd harmonics, that is, the square wave of 1Hz, and its spectrum contains 1Hz, 3Hz, 5Hz and other components, so if the high-frequency harmonic components are lost too much in the filtering process, it will lead to the illustrated results.
In order to filter out the 50Hz single frequency signal, it is suggested to use a band-stop filter to make the stop band fall within the spectrum gap of the square wave as much as possible.