Folium: TimesliderChoropleth not showing date in map header

Created on 1 May 2018  路  2Comments  路  Source: python-visualization/folium

Following the timesliderchoropleth tutorial, the code and slider work fine, except for the time-value on top of the map output. It stays at Wed Dec 31 1969 as I slide it. The output map appears to work fine as I see the colours on the choropleth changing. However, in the tutorial, the time-value is supposed to change to reflect the date.

I also noticed that the code below yields a different output in my Jupyter Notebook:

import pandas as pd

n_periods, n_sample = 48, 40

assert n_sample < n_periods

dt_index = pd.date_range(
    '2016-1-1', periods=n_periods, freq='M'
).strftime('%s')

dt_index

Output:
array(['2016-01-31 00:00:00', '2016-02-29 00:00:00', '2016-03-31 00:00:00',
'2016-04-30 00:00:00', .......'2019-08-31 00:00:00', '2019-09-30 00:00:00',
'2019-10-31 00:00:00', '2019-11-30 00:00:00', '2019-12-31 00:00:00'],
dtype='

Tutorial shows the output should be a datetime in seconds format:

array(['1454205600', '1456714800', .......1564542000', '1567220400',
'1569812400', '1572487200', '1575079200', '1577757600'],
dtype='

I am unsure if that issue is related but thought I'd bring it up.

Thanks

Output of folium version: 0.5.0+105.g065f6f3

bug

Most helpful comment

I was able to reproduce your problem, it also happens outside of Jupyter. You're right about the difference in the values in dt_index, and that's also the problem.

pandas.date_range() returns a DatetimeIndex object. That calls has a strftime method that expects a standard Python datetime formatter, which as far as I know doesn't have a unix timestamp directive. So if using '%s' worked before, it seems undocumented behavior.

A quick solution I found is to convert the DatetimeIndex object to integer values (unix time in nanoseconds) and then to strings:

datetime_index = pd.date_range('2016-1-1', periods=n_periods, freq='M')
dt_index_ints = datetime_index.astype(int) // 10**9
dt_index = dt_index_ints.astype(str)

Can you check if that works for you? Are there other solutions for this?

All 2 comments

I was able to reproduce your problem, it also happens outside of Jupyter. You're right about the difference in the values in dt_index, and that's also the problem.

pandas.date_range() returns a DatetimeIndex object. That calls has a strftime method that expects a standard Python datetime formatter, which as far as I know doesn't have a unix timestamp directive. So if using '%s' worked before, it seems undocumented behavior.

A quick solution I found is to convert the DatetimeIndex object to integer values (unix time in nanoseconds) and then to strings:

datetime_index = pd.date_range('2016-1-1', periods=n_periods, freq='M')
dt_index_ints = datetime_index.astype(int) // 10**9
dt_index = dt_index_ints.astype(str)

Can you check if that works for you? Are there other solutions for this?

Amazing. This works perfectly, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

themiurgo picture themiurgo  路  19Comments

FlorianHoevelmann picture FlorianHoevelmann  路  14Comments

achourasia picture achourasia  路  15Comments

achourasia picture achourasia  路  19Comments

reaganch picture reaganch  路  13Comments