Skip to content

Bug using fillna after resample.  #16566

Closed
@eoincondron

Description

@eoincondron

Code Sample, a copy-pastable example if possible

df = pd.DataFrame([[1], [2]], DatetimeIndex(['20170101', '20170103']))
df.resample('D').last().fillna(0)
                  0
2017-01-01	1.0
2017-01-02	NaN
2017-01-03	2.0

Problem description

fillna doesn't work as expected for some strange reason here. Multiplying by 1 seems "fix"things:

(1 * df.resample('D').last()).fillna(0)
                  0
2017-01-01	1.0
2017-01-02	0.0
2017-01-03	2.0

but using astype does not

df.resample('D').last().astype(float).fillna(0)
                  0
2017-01-01	1.0
2017-01-02	NaN
2017-01-03	2.0

unless using a mapping

df.resample('D').last().astype({0: float)}.fillna(0)
                  0
2017-01-01	1.0
2017-01-02	0.0
2017-01-03	2.0

isnull gives the expected output:

df.resample('D').last().isnull()
	0
2017-01-01	False
2017-01-02	True
2017-01-03	False

Constructing a the data frame directly gives the expected output:

pd.DataFrame([[1], [np.nan], [2]
    ], pd.DatetimeIndex(['20170101', '20170102', '20170103'], freq='D')).fillna(0)
	0
2017-01-01	1.0
2017-01-02	0.0
2017-01-03	2.0

Expected Output

df = pd.DataFrame([[1], [2]], DatetimeIndex(['20170101', '20170103']))
df.resample('D').last().fillna(0)
0
2017-01-01 1.0
2017-01-02 0.0
2017-01-03 2.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-327.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.20.1
pytest: 3.0.3
pip: 8.1.2
setuptools: 27.2.0
Cython: None
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 5.1.0
sphinx: None
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: None
tables: 3.3.0
numexpr: 2.6.2
feather: None
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: 1.0.13
pymysql: 0.7.6.None
psycopg2: None
jinja2: 2.8
s3fs: None
pandas_gbq: None
pandas_datareader: 0.2.1

# Paste the output here pd.show_versions() here

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions