Here, $\cos (-\frac{\pi}{2})=\cos (\frac{\pi}{2})=0$ and $\sin (-\frac{\pi}{2})=-1,~ \sin (\frac{\pi}{2})=1$
$\displaystyle \lim_{x \to \frac{\pi^-}{2}} \tan x = \lim_{x \to \frac{\pi^-}{2}} \frac{\sin x}{\cos x}=\frac{+1}{0^+}\to +\infty$
Again
$\displaystyle \lim_{x \to -\frac{\pi^+}{2}} \tan x = \lim_{x \to -\frac{\pi^+}{2}} \frac{\sin x}{\cos x}=\frac{-1}{0^+}\to -\infty$
$$\therefore f(x)=\tan x$$
So the correct answer is B.