Open In App

Python | Pandas Series.to_dense()

Improve
Improve
Like Article
Like
Save
Share
Report

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.

Pandas Series.to_dense() function return dense representation of NDFrame (as opposed to sparse). This basically mean that memory will be allocated to store even the missing values in the dataframe.

Syntax: Series.to_dense()

Parameter : None

Returns : Dense series

Example #1: Use Series.to_dense() function to convert the given series object to dense series object.




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow'])
  
# Create the Datetime Index
didx = pd.DatetimeIndex(start ='2014-08-01 10:00', freq ='W'
                     periods = 6, tz = 'Europe/Berlin'
  
# set the index
sr.index = didx
  
# Print the series
print(sr)


Output :

Now we will use Series.to_dense() function to achieve the conversion of the given Series object to dense series object.




# convert to dense object
sr.to_dense()


Output :

<

As we can see in the output, the Series.to_dense() function has returned the dense representation of the given series object. If we notice our series object does not have any missing values for this reason both the outputs looks the same. Let’s see another example which contains some missing values.
 
Example #2: Use Series.to_dense() function to convert the given series object to dense series object.




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([19.5, 16.8, None, 22.78, None, 20.124, None, 18.1002, None])
  
# Print the series
print(sr)


Output :

But, before we go ahead let’s convert the given series object to SparseSeries object to see the difference between the sparse and dense versions.

Now we will use Series.to_sparse() function to achieve the conversion of the given Series object to SparseSeries object.




# convert to Sparse object
sr.to_sparse()


Output :

As we can see in the output, the Series.to_sparse() function has successfully converted the given series object to sparseseries object. If we look at the bottom two lines, it has returned the info about memory Block location and the number of values contained in those blocks.

Now we will use Series.to_dense() function to achieve the conversion of the given Series object to dense series object.




# convert to dense object
sr.to_dense()


Output :

As we can see in the output, the Series.to_dense() function has returned the dense representation of the given series object. It has allocated memory to store even the missing values in the Series. Dense representation is not memory efficient when lots of data is missing.



Last Updated : 05 Feb, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads