Can Pandas plot a histogram of dates?

前端 未结 8 1449
我在风中等你
我在风中等你 2020-11-28 03:15

I\'ve taken my Series and coerced it to a datetime column of dtype=datetime64[ns] (though only need day resolution...not sure how to change).

i         


        
8条回答
  •  [愿得一人]
    2020-11-28 04:04

    Rendered example

    Example Code

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    """Create random datetime object."""
    
    # core modules
    from datetime import datetime
    import random
    
    # 3rd party modules
    import pandas as pd
    import matplotlib.pyplot as plt
    
    
    def visualize(df, column_name='start_date', color='#494949', title=''):
        """
        Visualize a dataframe with a date column.
    
        Parameters
        ----------
        df : Pandas dataframe
        column_name : str
            Column to visualize
        color : str
        title : str
        """
        plt.figure(figsize=(20, 10))
        ax = (df[column_name].groupby(df[column_name].dt.hour)
                             .count()).plot(kind="bar", color=color)
        ax.set_facecolor('#eeeeee')
        ax.set_xlabel("hour of the day")
        ax.set_ylabel("count")
        ax.set_title(title)
        plt.show()
    
    
    def create_random_datetime(from_date, to_date, rand_type='uniform'):
        """
        Create random date within timeframe.
    
        Parameters
        ----------
        from_date : datetime object
        to_date : datetime object
        rand_type : {'uniform'}
    
        Examples
        --------
        >>> random.seed(28041990)
        >>> create_random_datetime(datetime(1990, 4, 28), datetime(2000, 12, 31))
        datetime.datetime(1998, 12, 13, 23, 38, 0, 121628)
        >>> create_random_datetime(datetime(1990, 4, 28), datetime(2000, 12, 31))
        datetime.datetime(2000, 3, 19, 19, 24, 31, 193940)
        """
        delta = to_date - from_date
        if rand_type == 'uniform':
            rand = random.random()
        else:
            raise NotImplementedError('Unknown random mode \'{}\''
                                      .format(rand_type))
        return from_date + rand * delta
    
    
    def create_df(n=1000):
        """Create a Pandas dataframe with datetime objects."""
        from_date = datetime(1990, 4, 28)
        to_date = datetime(2000, 12, 31)
        sales = [create_random_datetime(from_date, to_date) for _ in range(n)]
        df = pd.DataFrame({'start_date': sales})
        return df
    
    
    if __name__ == '__main__':
        import doctest
        doctest.testmod()
        df = create_df()
        visualize(df)
    

提交回复
热议问题