I am very curious why I can assign value to a slice, but cannot print it out directly. Following codes shows details:
import pandas as pd
import numpy as np
from datetime import datetime
dt_start = datetime.strptime("20171010", "%Y%m%d")
dt_end = datetime.strptime("20171020", "%Y%m%d")
df = pd.DataFrame(np.nan, index=pd.date_range(start=dt_start, end=dt_end), columns=['sales', 'account'])
df.loc[:1, 'sales'] = 100 # works well
print(df.loc[:1, 'sales']) # error, why???
Error message:
TypeError: cannot do slice indexing on class 'pandas.tseries.index.DatetimeIndex with these indexers [1] of class 'int'
Why I can assign value but cannot print this slice?
Thanks very much for checking.