Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert timestamp into string in Python

I have a problem with the following code. I get an error "strptime() argument 1 must be str, not Timestamp"

I guess that what I should do is to convert date from timestamp to string but I do not know what to do.

class TweetAnalyzer:

    def tweets_to_data_frame(self,ElonMuskTweets):

        df = pd.DataFrame(data=[tweet.text for tweet in ElonMuskTweets],columns=['Tweets'])

        df['Text length'] = np.array ([len(tweet.text)for tweet in ElonMuskTweets])
        df['Date and time of creation'] = np.array ([tweet.created_at for tweet in ElonMuskTweets])
        df['Likes'] = np.array ([tweet.favorite_count for tweet in ElonMuskTweets])
        df['Retweets'] = np.array ([tweet.retweet_count for tweet in ElonMuskTweets])

        list_of_dates = []   
        list_of_times = []

        for date in df['Date and time of creation']:

            date_time_obj = datetime.strptime(date, '%Y-%m-%d %H:%M:%S') 
            list_of_dates.append(date_time_obj.date())  
            list_of_times.append(date_time_obj.time())

            df['Date'] = list_of_dates
            df['Time'] = list_of_times

            df['Date'] = pd.to_datetime(df['Date'])

            start_date = '2018-04-13'
            end_date = '2019-04-13'

            mask1 = (df['Date'] >= start_date) & (df['Date'] <= end_date)
            MuskTweets18_19 = df.loc[mask1]  

            return MuskTweets18_19.to_csv ('elonmusk_tweets.csv',index=False)

I get the error in

date_time_obj = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')

How can I solve this prolem? Thank you in advance

like image 579
Rachele Povelato Avatar asked May 01 '19 19:05

Rachele Povelato


People also ask

How do I convert datetime to string in Python?

To convert Python datetime to string, use the strftime() function. The strftime() method is a built-in Python method that returns the string representing date and time using date, time, or datetime object.


3 Answers

If it says "strptime() argument 1 must be str, not Timestamp", likely that you already have the pandas.Timestamp object, i.e., it is not a string but a parsed date time, only it is in Pandas' format, not Python's. So to convert, use this:

date_time_obj = date.to_pydatetime()

instead of date_time_obj = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')

like image 53
adrtam Avatar answered Oct 18 '22 20:10

adrtam


Can you coerce the data type to a string to perform this calculation?

date_time_obj = datetime.strptime(str(date), '%Y-%m-%d %H:%M:%S') 
like image 14
Paul Wildenhain Avatar answered Oct 18 '22 21:10

Paul Wildenhain


If the object is a Python Timestamp, you can implement:

timestamp = Timestamp('2017-11-12 00:00:00')

str_timestamp = str(timestamp)
like image 2
Nicolás Fornasari Avatar answered Oct 18 '22 21:10

Nicolás Fornasari