I have a dataframe you can have it by following code:

```
import numpy as np
import pandas as pd
from io import StringIO
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
df4s = """
    contract  RB  BeginDate  
0  A00118     46   20120705      
1  A00118     47   20121005      
2  A00253     48   0             
3  A00253     48   0                     
"""
df4 = pd.read_csv(StringIO(df4s.strip()), sep='\s+', 
                  dtype={"BeginDate": int}
                  )

```

The output is:

```
contract    RB  BeginDate
0   A00118  46  20120705
1   A00118  47  20121005
2   A00253  48  0
3   A00253  48  0

```

Now I want to generate a new header 'first\_month' based on the 'BeginDate',the logic is simple,
if the BeginDate equals 0 then first\_month will be 0,or it will equals to the month value of the BeginDate,my code is:

```
df4['first_month'] = np.where(df4['BeginDate'] != 0,
                                    df4['BeginDate'].astype(str).str[4:6:1].astype(int), 0)

```

The error is:

```
ValueError: invalid literal for int() with base 10: ''

```

The error track is:

```
:\ProgramData\Anaconda3\lib\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy)
    707         # work around NumPy brokenness, #1987
    708         if np.issubdtype(dtype.type, np.integer):
--> 709             return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
    710 
    711         # if we have a datetime/timedelta array of objects

pandas\_libs\lib.pyx in pandas._libs.lib.astype_intsafe()

pandas/_libs/src\util.pxd in util.set_value_at_unsafe()

ValueError: invalid literal for int() with base 10: ''

```

The output should be:

```
contract    RB  BeginDate  first_month
0   A00118  46  20120705   7
1   A00118  47  20121005   10
2   A00253  48  0          0
3   A00253  48  0          0

```

Any friend can help?